How do I set up CMake to generate header-only projects?

前端 未结 3 1954

I want to set up header-only C++ (or C) library projects, but can\'t find a clean way.

After some searches I\'ve found that you can\'t set up a normal library using

3条回答
  •  礼貌的吻别
    2020-12-24 01:49

    You can do this using the recent Interface Library feature:

    add_library(mylib INTERFACE)
    target_include_directories(mylib INTERFACE my_include_dir1 my_include_dir2)
    

    This creates a library target without any source files, and adds the include directories to the INTERFACE_INCLUDE_DIRECTORIES property of the target. This means that any target that links to this library will get these directories as include paths (-I) when built.

    For instance, to use the library with an executable target, just do:

    add_executable(myexec ${MY_SOURCES})
    target_link_libraries(myexec mylib)
    

提交回复
热议问题