CMAKE:target_include_directories和include_directories

时间:2018-08-22 06:23:42

标签: cmake

我正在读取一个大项目的cmakelist文件,并对target_include_directoriesinclude_directories的用法感到困惑。

include_directories应该能够将所有头文件添加到编译器搜索的路径中。但是我仍然在子目录的cmakelist文件中看到一些target_include_directories,它们为特定目标指定了一些包含路径。

有人可以解释这两个的用法吗?就我而言,将头文件的所有路径添加到include_directories就足够了。

1 个答案:

答案 0 :(得分:3)

include_directories适用于特定CMakeLists.txt文件中的所有目标。例如,假设您有

include_directories( ../include
   ${SOME_OTHER_PATH}/include
)

add_library(math ${MATH_SOURCES})

target_include_directories(math 
    math_include
)

add_executable(calculator ${MYCALCULATOR_SOURCES})   

target_include_directories(calculator 
    calc_include
)

calculator是可执行目标,而math是在同一CMakeLists.txt中定义的库目标。文件夹../include${SOME_OTHER_PATH}/include均可见。这意味着,当编译源时,cmake将为这两个目标添加选项-I../include -I<expanded-some-other-path>/include

对于target_include_directories,包含路径calc_include仅适用于calculator目标,而math_include仅适用于math目标。如指定的那样,math_includecalc_include(通常)将是包含CMakeLists.txt文件的同一文件夹中的子文件夹。