检索CMake中的所有链接标志 [英] Retrieve all link flags in CMake
问题描述
在CMake中,是否可以以编程方式检索将用于给定目标的链接器标志的完整列表?我能看到的唯一方法是检查目标 CMakeFiles
目录中的 link.txt
文件。不太理想。
In CMake, is it possible to programmatically retrieve the complete list of linker flags that will be used for a given target? The only way I can see to do this is to inspect the link.txt
file in the target's CMakeFiles
directory. Not ideal.
我感兴趣的用例是收集数据,以将其包含在pkg-config文件之类的文件中。我正在编写一个库,其中包含几个使用该库的可执行实用程序。构建可执行文件(尤其是在静态构建库时)需要一条非平凡的链接行来链接到我的库及其依赖项。因此,我想写出将这些可执行文件构建到程序包中包含的数据文件所必需的链接行,以便其他客户端可以知道如何链接。
The use case that I'm interested in is to collect the data to include in something like a pkg-config file. I'm writing a library, and it includes a couple executable utilities that use the library. Building the executables (especially when the library is build statically) requires a non-trivial link line to link to my library and its dependencies. So I'd like to write out the link line necessary for building these executables to a data file included with the package such that other clients can know how to link.
推荐答案
正如@Tsyvarev所评论的那样,没有内置命令或属性可以通过编程方式检索以下内容的完整列表:链接器标志 。
As @Tsyvarev has commented there is no build-in command or property "to programmatically retrieve the complete list of linker flags" in CMake.
但是受到您的提示的启发,所以我想写出将这些可执行文件构建到数据文件所需的链接行 认为我找到了一个可行的解决方案(至少对于makefile生成器而言)。
But inspired by your hint "so I'd like to write out the link line necessary for building these executables to a data file" I think I found a feasible solution (at least for makefile generators).
如果我正确理解了您的请求,我们不会在谈论像您通过eg获得的简单详细输出 CMAKE_VERBOSE_MAKEFILE
,仍然需要您手动复制内容。
And if I understand your request correctly, we are not talking about simple verbose outputs like you get with e.g. CMAKE_VERBOSE_MAKEFILE
, which would still need you to copy things manually.
因此,请考虑以下因素:
So taking the following into account:
- 您需要先运行生成器才能获得真实的链接行
- CMake允许您按名称发明任何链接器语言
- 您可以使用 CMAKE_> LANG< _LINK_EXECUTABLE 使用变量和扩展规则
- You need to run the generator first to get the real link line
- CMake allows you to invent any linker language by name
- You can define the link line with CMAKE_>LANG<_LINK_EXECUTABLE using variables and expansion rules
我想出了用来添加 LinkLine
可执行文件的方法。旨在创建我选择的链接行文件:
I came up with adding an LinkLine
executable using my ECHO
"linker" with the single purpose to create a link line file of my choosing:
set(CMAKE_ECHO_STANDARD_LIBRARIES ${CMAKE_CXX_STANDARD_LIBRARIES})
set(CMAKE_ECHO_FLAGS ${CMAKE_CXX_FLAGS})
set(CMAKE_ECHO_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS})
set(CMAKE_ECHO_IMPLICIT_LINK_DIRECTORIES ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})
set(
CMAKE_ECHO_LINK_EXECUTABLE
"<CMAKE_COMMAND> -E echo \"<FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>\" > <TARGET>"
)
add_executable(LinkLine "")
target_link_libraries(LinkLine MyLibraryTarget)
set_target_properties(
LinkLine
PROPERTIES
LINKER_LANGUAGE ECHO
SUFFIX ".txt"
)
这种方法的好处是,我的 LinkLine
目标的输出可以用作任何其他官方生成的可执行输出(例如,在 install()
命令或使用生成器表达式或构建后的步骤):
The nice thing about this approach is, that the output of my LinkLine
target can be used as any other "officially generated" executable output (e.g. in install()
commands or post-build steps with generator expressions):
add_custom_command(
TARGET LinkLine
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:LinkLine> PackageCfg/$<TARGET_FILE_NAME:LinkLine>
)
参考
- Recursive list of LINK_LIBRARIES in CMake
- add_custom_command is not generating a target
这篇关于检索CMake中的所有链接标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!