如何指定导入的OBJECT库依赖关系? [英] How to specify imported dependencies of an OBJECT library?
问题描述
我有一个对象
库 objlib
,该库已链接到主要目标 maintarget
。 objlib
有一个依赖库,例如 ZLIB
。如果我们使用旧版< package-name> _ *
变量,则很简单:
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
不能与对象
库:
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
不能获取包含目录es:
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
库的属性,并重新生成< package-name> _中通常可用的信息。 *
变量。
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屋!