target_link_libraries和INCLUDE_DIRECTORIES属性 [英] target_link_libraries and INCLUDE_DIRECTORIES property
问题描述
此代码段中
cmake_minimum_required(VERSION 3.0)
project(hello LANGUAGES C VERSION 0.0.1)
add_library(a INTERFACE)
target_include_directories(a INTERFACE /usr/local/include)
add_executable(b main.c)
target_link_libraries(b PUBLIC a)
get_target_property(dirs b INCLUDE_DIRECTORIES)
message(STATUS "dirs: ${dirs}")
CMake将打印:
-- dirs: dirs-NOTFOUND
我要全部包含目标的目录,但显然是通过 target_link_libraries
添加的目录不知何故。如何获取目标的所有包含目录?
I want all include directories of a target, but apparently those added via target_link_libraries
get ignored somehow. How can I obtain all include directories of a target?
推荐答案
问题是,您要查找的信息仅在生成步骤。您只能通过例如 add_custom_target()
调用,它们在编译时运行。您的 get_target_property()
和 message()
调用在CMake配置步骤中运行。
The problem is that the information you are seeking is only available after the generation step. You can get those only with e.g. add_custom_target()
calls, which run during compile time. Your get_target_property()
and message()
calls run during CMake configuration step.
免责声明:取自以下链接的问题的一些小修改:
Disclaimer: Taken with small modifications from the question linked below:
add_custom_command(
b_lists ALL
${CMAKE_COMMAND} -E echo "b INCLUDE_DIRECTORIES: $<TARGET_PROPERTY:B,INCLUDE_DIRECTORIES>"
)
参考
- CMake中LINK_LIBRARIES的递归列表
- < $ b是href = https://stackoverflow.com/questions/37900332/target-include-directories-prior-to-2-8-12> target_include_directories在2.8.12之前?
- Recursive list of LINK_LIBRARIES in CMake
- target_include_directories prior to 2.8.12?
这篇关于target_link_libraries和INCLUDE_DIRECTORIES属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!