C++ 使用 CMake 打开链接器标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3544245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turning on linker flags with CMake
提问by Clark Gaebel
When generating VS2010 targets with CMake, I would like the /LTCG flag turned on (only for release + releasewithdebinfo if possible, but its okay if its on for debug builds). How do I modify the linker flags? add_definitions()
doesn't work because that only modifies compiler flags. And yes, I have wrapped it in if(MSVC).
使用 CMake 生成 VS2010 目标时,我希望打开 /LTCG 标志(如果可能,仅适用于 release + releasewithdebinfo,但如果它打开用于调试版本就可以了)。如何修改链接器标志?add_definitions()
不起作用,因为那只会修改编译器标志。是的,我已经将它包装在 if(MSVC) 中。
How do I modify the linker flags?
如何修改链接器标志?
回答by DMags
You can modify the linker flags in MSC using #pragma comment(linker, ...)
您可以使用 #pragma comment(linker, ...) 修改 MSC 中的链接器标志
However, if you'd like to do it in the build process with cmake, here are the names you need to know:
但是,如果您想在使用 cmake 的构建过程中执行此操作,以下是您需要知道的名称:
CMAKE_EXE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_EXE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
(Thanks to Cmake.org).
(感谢Cmake.org)。
回答by elegant dice
and STATIC_LIBRARY_FLAGS http://www.cmake.org/cmake/help/v2.8.8/cmake.html#prop_tgt:STATIC_LIBRARY_FLAGS
和 STATIC_LIBRARY_FLAGS http://www.cmake.org/cmake/help/v2.8.8/cmake.html#prop_tgt:STATIC_LIBRARY_FLAGS
for static libraries
对于静态库
回答by DiB
The use of the "ucm" library seems like a nice approach. I rolled a simple macro that help me uniformly manage linker flags in CMake for all configurations while also allowing compiler-specific use. (Just setting the variable can cause flags to stack up when CMake is configured multiple times.)
使用“ucm”库似乎是一个不错的方法。我推出了一个简单的宏,它帮助我统一管理 CMake 中所有配置的链接器标志,同时还允许特定于编译器的使用。(在多次配置 CMake 时,仅设置变量会导致标志堆积。)
macro(ADD_MSVC_LINKER_FLAG flag)
if(MSVC)
if(${CMAKE_EXE_LINKER_FLAGS} MATCHES "(${flag}.*)")
# message("skipping linker flags")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}" CACHE STRING "Linker Flags for Release Builds" FORCE)
endif()
if(${CMAKE_SHARED_LINKER_FLAGS} MATCHES "(${flag}.*)")
# message("skipping linker flags")
else()
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${flag}" CACHE STRING "Linker Flags for Release Builds" FORCE)
endif()
if(${CMAKE_STATIC_LINKER_FLAGS} MATCHES "(${flag}.*)")
# message("skipping linker flags")
else()
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} ${flag}" CACHE STRING "Linker Flags for Release Builds" FORCE)
endif()
if(${CMAKE_MODULE_LINKER_FLAGS} MATCHES "(${flag}.*)")
# message("skipping linker flags")
else()
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${flag}" CACHE STRING "Linker Flags for Release Builds" FORCE)
endif()
endif()
endmacro()
Other compilers are then supported by creating a compiler-specific macro that checks for the compiler to be in use. That makes it harder to set the right flag on the wrong compiler.
然后通过创建特定于编译器的宏来支持其他编译器,该宏检查要使用的编译器。这使得在错误的编译器上设置正确的标志变得更加困难。
if(CMAKE_COMPILER_IS_GNUCXX)
and
和
if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
回答by kaveish
You can add linker flags for a specific target using the LINK_FLAGS
property:
您可以使用以下LINK_FLAGS
属性为特定目标添加链接器标志:
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " ${flag}")
(note that I added a space before the flag since I'm using APPEND_STRING
)
(请注意,由于我正在使用,我在标志前添加了一个空格APPEND_STRING
)
回答by onqtam
For adding linker flags - the following 4 CMake variables:
添加链接器标志 - 以下 4 个 CMake 变量:
CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS
can be easily manipulated for different configs (debug, release...) with the ucm_add_linker_flagsmacro of ucm
可以使用ucm的ucm_add_linker_flags宏针对不同的配置(调试、发布...)轻松操作
linker flags can also be managed on a per-target basis - by using target_link_librariesand passing flags with a -
in front of them (but not with a -l
- that would be treated as a link library and not a link flag).
链接器标志也可以在每个目标的基础上进行管理 - 通过使用target_link_libraries并-
在它们前面传递标志(但不是带有-l
- 将被视为链接库而不是链接标志)。