CMake, how to include headers without sources?

This is probably a bogus question, but I literally looked at the first two pages of Google without success. I am writing only the header library, and I can’t correctly configure CMake so that when building my solution, this one will be main.cppsuitable. How can I do that?

EDIT

Therefore, I probably should give a slightly more detailed explanation.
Let's say I have a folder ./srcwith: ./src/coreand ./src/wrappers. Inside each folder, I have files .hthat should be included in the file main.cpp:

#include <src/core/reader.h>

Also, when I entered CMakeList.txtsomething like:

include_directories(src/core)
add_executable(main main.cpp)

I get a message like: src/core/reader.hthere is no such file or directory.

+4
4

, src.
, CMakeLists.txt src, :

include_directories(${CMAKE_SOURCE_DIR})

CMAKE_SOURCE_DIR:

.

src , - :

#include <src/whatever/you/want.h>

, :

  • :

    include_directories(${CMAKE_SOURCE_DIR}/src)
    

    :

    #include <whatever/you/want.h>
    

    src .

  • , , target_include_directories include_directories , :

    target_include_directories(main ${CMAKE_SOURCE_DIR}/src)
    

    add_executable, .

+4

include_directories(), , .


.

 #include <src/core/reader.h>

 include_directories(/full_parent_path_of_src)
+1

, CMakeLists.txt include_directories(<DIRECTORY>) .

0

, , :

add_library(headerlib INTERFACE)
target_include_directories(headerlib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

:

target_link_libraries(mytarget headerlib)

, , .

0

Source: https://habr.com/ru/post/1658722/


All Articles