如何指定 OBJECT 库的导入依赖项? [英] How to specify imported dependencies of an OBJECT library?
问题描述
我有一个 OBJECT
库 objlib
,它链接到主要目标 maintarget
.objlib
有一个依赖库,比如 ZLIB
.如果我们使用旧的
变量,那么这很容易:
I have an OBJECT
library objlib
which is linked into the main target maintarget
. The objlib
has a dependent library, say, ZLIB
. If we're using the legacy <package-name>_*
variables then it's easy:
add_library(objlib OBJECT ...)
target_include_directories(objlib ${ZLIB_INCLUDE_DIRS})
...
add_executable(maintarget $<TARGET_OBJECTS:objlib>)
target_link_libraries(maintarget ${ZLIB_LIBRARIES})
但我想将依赖项用作 IMPORTED
库,因为它更简洁(以及创建配置模块的便捷方式,即使用 install(EXPORT ...)
,就是这样做的).
But I want to use the dependency as an IMPORTED
library because it's more concise (and the convenient way to create config modules, that is, using install(EXPORT ...)
, does just that).
以下代码不起作用,因为 target_link_libraries
不能与 OBJECT
库一起使用:
The following code does not work because target_link_libraries
cannot be used with an OBJECT
library:
add_library(objlib OBJECT ...)
target_link_libraries(objlib ZLIB::ZLIB)
将 ZLIB::ZLIB
链接到 maintarget
也不起作用,objlib
没有获得包含目录:
Linking ZLIB::ZLIB
to maintarget
does not work either, objlib
does not get the include directories:
add_library(objlib OBJECT ...)
...
add_executable(maintarget $<TARGET_OBJECTS:objlib>)
target_link_libraries(maintarget ZLIB::ZLIB)
使用中间 INTERFACE
库 (objlib-wrapper
) 进行黑客攻击也不起作用.
Hacking with an intermediate INTERFACE
library (objlib-wrapper
) does not work either.
唯一有效的是查询 IMPORTED
库的属性并重新生成通常在
变量中可用的信息.这是一个令人讨厌的解决方法.
The only thing that works is to query the IMPORTED
library's properties and regenerate the information normally available in the <package-name>_*
variables. Which is a nasty workaround.
有更好的方法吗?
推荐答案
从 CMake 3.12 开始,您现在可以在对象库上使用 target_link_libraries
来获取使用要求.
As of CMake 3.12, you can now use target_link_libraries
on object libraries to get usage requirements.
使用 3.12,您提到的这种方法应该可行:
Using 3.12, this approach that you mentioned should work:
add_library(objlib OBJECT ...)
target_link_libraries(objlib ZLIB::ZLIB)
这篇关于如何指定 OBJECT 库的导入依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!