CMake属性和扩展生成器表达式 [英] CMake properties and expanding generator expressions

查看:1751
本文介绍了CMake属性和扩展生成器表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图结合几个静态库与CMake和我发现一个 answer 建议执行以下操作:

I am trying to combine several static libraries with CMake and I found an answer that suggests doing the following:

SET_TARGET_PROPERTIES(merged PROPERTIES STATIC_LIBRARY_FLAGS "full\path\to\lib1.lib full\path\to\lib2.lib")

我想要组合的库是其他目标的输出,当我想引用目标的输出时,我通常使用生成器表达式,例如$< TARGET_FILE:MyLib>。然而在这个上下文中它不工作,因为生成器表达式在被传递给链接器之前不被扩展,并且它抱怨$<无法找到TARGET_FILE:MyLib>

The libraries I want to combine are the output of other targets and when I want to refer to the output of a target I usually use generator expression such as $< TARGET_FILE:MyLib>. However in this context it does not work because the generator expression does not get expanded before being passed to the linker and it complains that $< TARGET_FILE:MyLib> cannot be found.

这不起作用:

set_target_properties(merged PROPERTIES STATIC_LIBRARY_FLAGS $<TARGET_FILE:MyLib>)

将属性设置为库的路径:

I guess I am meant to set the property to the path of the library:

set_target_properties(merged PROPERTIES STATIC_LIBRARY_FLAGS ${CURRENT_BIN_DIR}/MyLib.lib})

手动生成此路径可能很繁琐(我有很多目标),我想知道如果给定目标

Generating this path manually can be tedious (I have many targets) and I was wondering if, given a target, there was a better way to get that path automatically.

例如(我正在编写这里的语法):

For example (I am making up the syntax here):

set_target_properties(merged PROPERTIES STATIC_LIBRARY_FLAGS EXPAND_PATH($<TARGET_FILE:MyLib>))


推荐答案

你是对的,生成器表达式将是正确的选择 - 尤其是在多配置环境中 - 但是如果某个命令或属性不支持它们,你不能欺骗他们进入工作。

You are right that generator expressions would have been the right choice - especially in multi-configuration environments - but if a certain command or property does not support them, you can't trick them into working.

但有其他选择:

设置 ARCHIVE_OUTPUT_DIRECTORY 属性

Setting ARCHIVE_OUTPUT_DIRECTORY Property

您可以稍微更改我想我的意思是策略,并将您要合并的库输出放入路径你自己选择。以下示例适用于像Visual Studio这样的多配置环境:

You could slightly change your "I guess I am meant to" strategy and place your library outputs you want to merge into a path of your own choosing. The following example is for a multi-configuration environment like Visual Studio:

add_library(lib1 STATIC bar.cc)
set_target_properties(lib1 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Libs")

file(WRITE dummy.cc "")
add_library(merged STATIC dummy.cc) 
add_dependencies(merged lib1)

set_target_properties(
    merged PROPERTIES 
        STATIC_LIBRARY_FLAGS_DEBUG   "${CMAKE_BINARY_DIR}/Libs/Debug/lib1.lib"
        STATIC_LIBRARY_FLAGS_RELEASE "${CMAKE_BINARY_DIR}/Libs/Release/lib1.lib"
)

使用 OBJECT 图书馆

Using OBJECT Libraries

或者您可以使用 OBJECT库作为中间分组目标,以防止欺骗CMake构建您的合并 library:

Or you can work with OBJECT libraries as intermediate grouping targets to prevent having to trick CMake into building your merged library:

add_library(lib1 OBJECT bar.cc)
add_library(lib2 OBJECT foo.cc)
add_library(merged $<TARGET_OBJECTS:lib1> $<TARGET_OBJECTS:lib2>) 

我个人更喜欢后者。

这篇关于CMake属性和扩展生成器表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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