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_definition()不起作用,因为它只修改编译器标志。是的,我把它包在if(MSVC)里了。
How do I modify the linker flags?
如何修改链接器标志?
5 个解决方案
#1
27
You can modify the linker flags in MSC using #pragma comment(linker, ...)
您可以使用#pragma注释修改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_EXE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
- CMAKE_SHARED_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
- CMAKE_MODULE_LINKER_FLAGS
(Thanks to Cmake.org).
(由于Cmake.org)。
#2
4
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
静态库
#3
2
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)
#4
0
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_flags macro of ucm
可以使用ucm的ucm_add_linker_flags宏轻松地为不同的配置(调试、发布…)进行操作吗
linker flags can also be managed on a per-target basis - by using target_link_libraries and 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和在其前面传递带有- a的标志(但不包括-l),这些标志将被视为链接库而不是链接标志)。
#5
0
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)
#1
27
You can modify the linker flags in MSC using #pragma comment(linker, ...)
您可以使用#pragma注释修改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_EXE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
- CMAKE_SHARED_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
- CMAKE_MODULE_LINKER_FLAGS
(Thanks to Cmake.org).
(由于Cmake.org)。
#2
4
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
静态库
#3
2
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)
#4
0
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_flags macro of ucm
可以使用ucm的ucm_add_linker_flags宏轻松地为不同的配置(调试、发布…)进行操作吗
linker flags can also be managed on a per-target basis - by using target_link_libraries and 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和在其前面传递带有- a的标志(但不包括-l),这些标志将被视为链接库而不是链接标志)。
#5
0
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)