how to install a header file using cmake

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 :

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 ?

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:

Not quite true, you could also use 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.

1 Like