问题描述
链接二进制文件时,我可以使用 CMAKE_EXE_LINKER_FLAGS
添加一个标志(比如说 -Wl-按需
).但是,如果我链接一个库,则不会考虑这个额外的标志.我需要类似 CMAKE_LIB_LINKER_FLAGS
的东西,但找不到.
我应该怎么做?
注意:现代CMake的解决方案比下面提到的更好(有关详细信息,请参阅更新).
您可以使用 CMAKE_SHARED_LINKER_FLAGS :
设置(CMAKE_SHARED_LINKER_FLAGS"-Wl,按需")
这个问题看起来像是相关的./p>
UPD
感谢@Bruce Adams指出,自v3.13以来,CMake具有特殊命令用于此目的: add_link_options
.
UPD 2
感谢@Alex Reinking指出现代CMake不建议使用全局设置.建议优先考虑全局设置之前的属性设置,因此,应该使用 target_link_options
代替具有全局范围的 add_link_options
.有关详细信息,请参见Alex的答案.
When linking a binary I can use CMAKE_EXE_LINKER_FLAGS
to add a flag (let's say -Wl,-as-needed
). However, if I link a library this extra flag will not be taken into account. I would need something like CMAKE_LIB_LINKER_FLAGS
but I can't find it.
How should I do this?
Note: modern CMake has a better solution than mentioned below (see updates for details).
You can use CMAKE_SHARED_LINKER_FLAGS like:
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
This question looks like related.
UPD
Thanks to @Bruce Adams who points out that since v3.13 CMake has special command for such purpose: add_link_options
.
UPD 2
Thanks to @Alex Reinking who points out that modern CMake doesn't recommend using global settings. It is suggested to give the preference to the property settings before the global ones, so instead of add_link_options
that has a global scope, the target_link_options
should be used. See Alex's answer for details.
这篇关于如何使用CMake为库添加链接器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!