简体   繁体   English

CMake 3.0 INTERFACE 类型的 add_library 中断 get_target_property

[英]CMake 3.0 add_library of type INTERFACE breaks get_target_property

I want to add a header only directory to a cmake project which I am using in Windows 7 with Visual Studio 2013 Update 2. I did some research and came up with this Cmakelists.txt file:我想将一个 header 目录添加到一个 cmake 项目中,我在 Windows 7 中使用 Visual Studio 2013 Update 2。我做了一些研究并提出了这个 Cmakelists.txt 文件:

add_library(AAA_lib INTERFACE)
target_include_directories(AAA_lib
    INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    )

This appears to work however in some of our internal cmake code we call get_target_property:这似乎有效,但是在我们调用 get_target_property 的一些内部 cmake 代码中:

get_target_property(target_libraries ${target} LINK_LIBRARIES)

This generates a CMake error:这会生成 CMake 错误:

CMake Error at x_common_libs/xml/xsdmap.cmake:41 (get_target_property):
  INTERFACE_LIBRARY targets may only have whitelisted properties.  The
  property "LINK_LIBRARIES" is not allowed.

It seems like an odd error since a get should only return targets with the appropriate property.这似乎是一个奇怪的错误,因为 get 应该只返回具有适当属性的目标。 Is this a bug in CMake?这是 CMake 中的错误吗? Or is there some other way that this should be implemented?或者还有其他方法可以实现吗?

Why do we need to call get_target_property?为什么我们需要调用 get_target_property? We have a cmake helper function which recursively goes through all of dependencies of a project building up a list of all dependent projects that require some custom post processing.我们有一个 cmake 助手 function,它递归地遍历项目的所有依赖项,构建需要一些自定义后处理的所有依赖项目的列表。 So if project X uses project B and C and C uses D, then we get a list of X, B, C, D and then check to see which directories match a requirement and then call a custom_command.因此,如果项目 X 使用项目 B,C 和 C 使用 D,那么我们将获得 X、B、C、D 的列表,然后检查哪些目录符合要求,然后调用 custom_command。

Thanks, Jason谢谢,杰森

Seems that it is CMake bug. 似乎是CMake错误。 There is new target type in CMake 3.0 called INTERFACE_LIBRARY. CMake 3.0中有一个新的目标类型,称为INTERFACE_LIBRARY。 You are supposed to use only some of properties for this type. 您应该只对这种类型使用某些属性。 However, I don't think it was expected to raise an error in case of reading other properties. 但是,我不认为在读取其他属性的情况下会引发错误。

I came out with the following workaround: 我提出了以下解决方法:

get_target_property(_TARGET_TYPE ${target} TYPE)
if(_TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
  unset(target_libraries)
else()
  get_target_property(target_libraries ${target} LINK_LIBRARIES)
endif()

This is a restriction that was lifted in CMake 3.19.这是在 CMake 3.19 中解除的限制。

See https://gitlab.kitware.com/cmake/cmake/-/issues/19145 for details.详情见https://gitlab.kitware.com/cmake/cmake/-/issues/19145

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CMake 项目结构:何时使用 add_library/target_link_library 而不是 target_sources? - CMake Project structure: When to use add_library/target_link_library over target_sources? cmake:add_library 无法创建目标“cxx”,因为已存在另一个同名目标 - cmake : add_library cannot create target “cxx” because another target with the same name already exists cmake add_library 具有最低 cpp 标准 - cmake add_library with minimum cpp standard CMake add_library包含其他库 - CMake add_library containing other libraries 为什么 CMake 没有与我的自定义库 (add_library/set_property) 链接? - Why is CMake not linking with my custom library (add_library/set_property)? 是否存在CMake add_library的环境变量( <lib> 宾语 <src> )? - Are there environment variables for CMake add_library(<lib> OBJECT <src>)? cmake add_library不初始化静态全局变量 - cmake add_library doesn't initialize static global variable 哪个变量用于CMake的ADD_LIBRARY函数的编译器标志? - Which variable for compiler flags of CMake's ADD_LIBRARY function? cmake 错误 [SDK/Util/CMakeLists.txt:132 (add_library) 处的 CMake 错误] - cmake error [CMake Error at SDK/Util/CMakeLists.txt:132 (add_library)] 如何在使用add_library不包括特定目标文件时告诉CMake - How to tell CMake when using add_library to not include a specific object file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM