# how to install a header file using cmake

**URL:** https://discourse.cmake.org/t/how-to-install-a-header-file-using-cmake/11557
**Category:** Usage
**Tags:** os:linux
**Created:** [August 30, 2024, 11:23am UTC](https://discourse.cmake.org/t/how-to-install-a-header-file-using-cmake/11557 "2024-08-30T11:23:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jonathan\_Dawsa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jonathan_dawsa/32/3695_2.png) [@Jonathan\_Dawsa](https://discourse.cmake.org/u/Jonathan_Dawsa)
#### Post date: [August 30, 2024, 11:23am UTC](https://discourse.cmake.org/t/how-to-install-a-header-file-using-cmake/11557/1 "2024-08-30T11:23:40Z")

</div>

hello i am learning to use cmake better and i am now trying to learn to install header files in the computer(linux mint os ) using vscode so that i can use it in different projects and i am struggling a little bit the folders is called: installoefenv1. It has a hpp file called: ihead1.hpp and a CMakeLists.txt in it the iihead1.hpp is written like this:  
`#include   
#include

using namespace std;  
string t1;

void incout (string t1 ) {

cout \<\< t1 \<\< ‘\n’;

}`

and the cmakelists is like :

````auto
cmake_minimum_required(VERSION 3.22.1) # the minimum requirement
project(installestv1) # project name
set(CMAKE_CXX_VERSION 17)
add_library(ihead1 STATIC ihead1.hpp) # added library
include(GNUInstallDirs)

install(TARGETS ihead1 PUBLIC_HEADER DESTINATION lib) # install target for lib file

install(FILES ihead1.hpp DESTINATION include) # install target for include

``` and when i try to install with cmake install or sudo make install i get this error:`[main] Building folder: /home/jonathan/Documents/cpp.test/install_oefen/build 
[main] Configuring project: install_oefen 
[proc] Executing command: /usr/bin/cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc-12 --no-warn-unused-cli -S/home/jonathan/Documents/cpp.test/install_oefen -B/home/jonathan/Documents/cpp.test/install_oefen/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] CMake Error: Cannot determine link language for target "ihead1".
[cmake] CMake Error: CMake can not determine linker language for target: ihead1
[cmake] CMake Generate step failed. Build files cannot be regenerated correctly.
[proc] The command: /usr/bin/cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc-12 --no-warn-unused-cli -S/home/jonathan/Documents/cpp.test/install_oefen -B/home/jonathan/Documents/cpp.test/install_oefen/build -G "Unix Makefiles" exited with code: 1
` can anyone help me ?
````

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [September 14, 2024, 7:14am UTC](https://discourse.cmake.org/t/how-to-install-a-header-file-using-cmake/11557/2 "2024-09-14T07:14:24Z")

</div>

> [@Jonathan\_Dawsa](#):
>
> `add_library(ihead1 STATIC ihead1.hpp) # added library`

You can’t create a static library without sources!

If you wand to create a header only library, you have to use:

`add_library(ihead1 INTERFACE ihead1.hpp) # header only library`

But you need also `target_include_directories()!`

see too this example:

> <https://github.com/ClausKlein/CMake-Best-Practices---2nd-Edition/blob/feature/format-cpp-code/chapter03/hello_header_only/CMakeLists.txt>

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 15, 2024, 5:55am UTC](https://discourse.cmake.org/t/how-to-install-a-header-file-using-cmake/11557/3 "2024-09-15T05:55:14Z")

</div>

> [@ClausKlein](#):
>
> But you need also `target_include_directories()!`

Not quite true, you could also use [file sets](https://cmake.org/cmake/help/git-master/command/target_sources.html#file-sets), which add header search paths. File sets should be preferred over `target_include_directories()` calls these days because file sets also take care of header search paths when the target is installed.
