cmake中INCLUDE_DIRECTORIES
时间: 2024-04-27 17:18:25 浏览: 160
在CMake中,INCLUDE_DIRECTORIES是一个用于指定头文件搜索路径的指令。它可以用来告诉编译器在哪里查找头文件。
使用INCLUDE_DIRECTORIES指令可以将一个或多个目录添加到头文件搜索路径中。这样,在编译过程中,编译器就能够找到这些头文件并正确地包含它们。
下面是INCLUDE_DIRECTORIES指令的使用示例:
```
INCLUDE_DIRECTORIES(directory1 directory2 ...)
```
其中,directory1、directory2等是要添加到头文件搜索路径中的目录路径。
需要注意的是,INCLUDE_DIRECTORIES指令是全局的,它会影响到整个项目中的所有源文件。因此,通常建议将INCLUDE_DIRECTORIES指令放在CMakeLists.txt文件的顶层位置,以确保所有源文件都能够正确地找到所需的头文件。
相关问题
在cmake中执行target_include_directories(mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) target_include_directories(mylib_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include )提醒正确,执行target_include_directories(mylib mylib_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include )时提醒错误 target_include_directories called with invalid arguments
执行 `target_include_directories(mylib mylib_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)` 时,会出现 "target_include_directories called with invalid arguments" 的错误提示,这是因为 `target_include_directories()` 函数不支持一次为多个目标添加头文件搜索路径。
如果想要为多个目标设置相同的头文件搜索路径,需要分别为每个目标调用 `target_include_directories()` 函数。例如,下面的代码将 `${CMAKE_CURRENT_SOURCE_DIR}/include` 目录添加到 `mylib` 和 `mylib_shared` 两个目标的头文件搜索路径中:
```
target_include_directories(mylib
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(mylib_shared
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
```
这样,这两个目标的依赖项就可以使用这些路径了。
需要注意的是,如果要使用不同的可见性(即 `INTERFACE`、`PUBLIC` 或 `PRIVATE`)为不同的目标添加头文件路径,需要为每个目标分别调用 `target_include_directories()` 函数,并指定相应的可见性。例如:
```
target_include_directories(mylib
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(mylib_shared
INTERFACE
/usr/local/include
)
```
这样,`mylib` 目标的头文件路径对其依赖项可见,而 `mylib_shared` 目标的头文件路径对其依赖项不可见,但对其接口(即 `INTERFACE`)可见。
将下面改写成linux中的cmakelists.txt:cmake_minimum_required(VERSION 3.20) project(07_opencl_conv_test) set(CMAKE_CXX_STANDARD 11) include_directories(include) include_directories(common/inc) link_directories(lib) include_directories(${INC_DIR}) message(STATUS "OpenCV libraries: ${INC_DIR}") link_directories(${INC_DIR}) link_libraries(opencl.lib) message(STATUS "OpenCV libraries: ${INC_DIR}") add_executable(07_opencl_conv_test main.cpp)
cmake_minimum_required(VERSION 3.20)
project(07_opencl_conv_test)
set(CMAKE_CXX_STANDARD 11)
include_directories(include)
include_directories(common/inc)
include_directories(${INC_DIR})
link_directories(lib)
link_directories(${INC_DIR})
link_libraries(opencl)
message(STATUS "OpenCV libraries: ${INC_DIR}")
add_executable(07_opencl_conv_test main.cpp)
阅读全文