CMake包括导入库的依赖项而无需链接 [英] CMake Include dependencies of imported library without linking

查看:151
本文介绍了CMake包括导入库的依赖项而无需链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMake 3.5.2。

I am using CMake 3.5.2.

请考虑以下情况。我有一个导入的库 Foo :: Foo

Consider the following situation. I have an imported library Foo::Foo:

add_library(Foo::Foo UNKNOWN IMPORTED)

此导入的库已填充了适当的属性:

This imported library has been populated with appropriate properties:

set_target_properties(Foo::Foo PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "/path/to/include/blah" "/another/path/include/other"
  IMPORTED_LINK_INTERFACE_LIBRARIES blah other
  IMPORTED_LOCATION "/path/to/libfoo.a-or-so")

我有一个名为 bar 的便利库。我需要它包含 Foo :: Foo 的包含目录,但我不希望它链接到 Foo :: Foo

I have a convenience library called bar. I need it to include Foo::Foo's include directories, but I do not want it to link against Foo::Foo.

add_library(bar STATIC "${BAR_SOURCES}")

如何仅添加 Foo :: Foo 中的include依赖项?这是我尝试失败的内容:

How can I add just the include dependencies from Foo::Foo? Here is what I have tried that has failed:

# This did not include any includes from Foo::Foo
target_link_libraries(bar INTERFACE Foo::Foo)

# This included only the first include directory from Foo::Foo
target_include_directories(bar PUBLIC "$<TARGET_PROPERTY:Foo::Foo,INTERFACE_INCLUDE_DIRECTORIES>")


推荐答案

我举了一个例子。您应该将示例中的代码更改为:

I have given you example a try. You should change the code in your example to:

set_target_properties(
   Foo::Foo PROPERTIES
       INTERFACE_INCLUDE_DIRECTORIES 
           "/path/to/include/blah;/path/to/include/other"
       IMPORTED_LINK_INTERFACE_LIBRARIES "blah.a"
       IMPORTED_LOCATION "/path/to/libfoo.a-or-so"
 )

set_target_properties() 仅接受属性 /值对(以空格作为分隔符)。而且您的示例并没有引发任何错误,因为您始终可以定义自己的属性(使用任何命名)。

The call to set_target_properties() only accepts "property" / "value" pairs (with spaces as delimiter). And your example just wasn't throwing any errors because you can always define your own properties (with any naming).

请将您的包含目录列表转移到 CMake列表中(用分号分隔的字符串)。

Please transfer your include directory list into a "CMake List" (string with semicolon separated elements).

替代

如果您只是想要重置传递库,您可以使用以下方法做到这一点:

If you just want to "reset" the transitive libraries you can do this with e.g.:

target_link_libraries(bar Foo::Foo)
set_target_properties(bar PROPERTIES INTERFACE_LINK_LIBRARIES "")

我在建立共享库时使用了这种方法库与我链接到的项目相同(我不希望共享库的库依赖性也使用共享库链接到目标)。

I've used this approach when I was building a shared library in the same project as I was linking against the same (and I did not want the library dependencies of the shared library also being linked into the target using the shared library).

参考

这篇关于CMake包括导入库的依赖项而无需链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆