target_include_directories 和 include_directories
时间: 2023-12-29 17:41:02 浏览: 80
target_include_directories和include_directories是CMake中用于包含头文件路径的命令。
include_directories命令用于指定编译过程中需要搜索头文件的路径。该命令可以接受多个参数,每个参数都是一个路径,指定要搜索的头文件目录。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* *2* *3* [CMake常用命令(七)include_directories和target_include_directories命令](https://blog.csdn.net/qq_26849933/article/details/127131908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
[ .reference_list ]
相关问题
target_include_directories 和include_directories
target_include_directories和include_directories都是在CMake中用于指定编译器搜索头文件的路径的指令。
target_include_directories用于为特定的目标(target)设置头文件搜索路径。具体语法为:
target_include_directories(<target> [SYSTEM [AFTER|BEFORE <INTERFACE|PUBLIC|PRIVATE> [items1...])
其中,<target>表示目标名称,表示将路径视为系统路径,用于指定添加路径的位置,<INTERFACE|PUBLIC|PRIVATE>表示添加路径的可见性, [SYSTEM dir1 [dir2 ...])
其中,用于指定添加路径的位置,表示将路径视为系统路径,dir1 [dir2 ...表示要添加的路径。
总结起来,target_include_directories用于为特定目标设置头文件搜索路径,而include_directories用于为当前目录及其子目录设置头文件搜索路径。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [cmake:target_include_directories、include_directories、add_executable](https://blog.csdn.net/zhizhengguan/article/details/115331314)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [include_directories和target_include_directories](https://blog.csdn.net/qq_33726635/article/details/121896441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
在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`)可见。
阅读全文