检索 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).
如果我正确理解您的请求,我们不是在谈论像您得到的简单详细的输出,例如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.
因此请考虑以下事项:
- 需要先运行生成器才能得到真正的链接线
- CMake 允许您按名称发明任何链接器语言
- 您可以使用 CMAKE_>LANG<_LINK_EXECUTABLE<定义链接行/a> 使用变量和扩展规则
我想出了使用我的 ECHO
链接器"添加一个 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>
)
参考资料
这篇关于检索 CMake 中的所有链接标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!