检索CMake中的所有链接标志 [英] Retreive 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.
因此,请考虑以下因素:
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
我想出了使用我的 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>
)
参考
- Recursive list of LINK_LIBRARIES in CMake
- add_custom_command is not generating a target
这篇关于检索CMake中的所有链接标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!