How to set include_directories from a CMakeLists.txt file?

后端 未结 1 1474
陌清茗
陌清茗 2020-12-15 08:19

I seem to be having trouble setting the include path (-I) using the include_directories() command in CMake. My project directory is as follows:

相关标签:
1条回答
  • 2020-12-15 08:51

    include_directories() populates a directory property called INCLUDE_DIRECTORIES:

    http://www.cmake.org/cmake/help/v2.8.12/cmake.html#prop_dir:INCLUDE_DIRECTORIES

    Note that CMake 2.8.11 learned the target_include_directories command, which populates the INCLUDE_DIRECTORIES target property. http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:target_include_directories

    Note also that you can encode the fact that 'to compile against the headers of the lib target, the include directory lib/inc is needed' into the lib target itself by using target_include_directories with the PUBLIC keyword.

    add_library(lib STATIC ${lib_hdrs} ${lib_srcs}) # Why do you list the headers?
    target_include_directories(lib PUBLIC "${ROOT_SOURCE_DIR}/lib/inc")
    

    Note also I am assuming you don't install the lib library for others to use. In that case you would need to specify different header directories for the build location and for the installed location.

    target_include_directories(lib
      PUBLIC
        # Headers used from source/build location:
        "$<BUILD_INTERFACE:${ROOT_SOURCE_DIR}/lib/inc>"
        # Headers used from installed location:
        "$<INSTALL_INTERFACE:include>"     
    )
    

    Anyway, that's only important if you are installing lib for others to use.

    After the target_include_directories(lib ...) above you don't need the other include_directories() call. The lib target 'tells' proj1 the include directories it needs to use.

    See also target_compile_defintions() and target_compile_options().

    0 讨论(0)
提交回复
热议问题