Turning on linker flags with CMake
Solution 1
You can modify the linker flags in MSC using #pragma comment(linker, ...)
However, if you'd like to do it in the build process with cmake, here are the names you need to know:
CMAKE_EXE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
(Thanks to Cmake.org).
Solution 2
and STATIC_LIBRARY_FLAGS http://www.cmake.org/cmake/help/v2.8.8/cmake.html#prop_tgt:STATIC_LIBRARY_FLAGS
for static libraries
Solution 3
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.)
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)
Solution 4
You can add linker flags for a specific target using the LINK_FLAGS
property:
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
)
Solution 5
For adding linker flags - the following 4 CMake variables:
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
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).
Comments
-
Clark Gaebel almost 2 years
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).How do I modify the linker flags?
-
koda almost 11 yearsIn recent CMake versions the only flag left is CMAKE_EXE_LINKER_FLAGS; the best thing to do is to edit the target properties (see stackoverflow.com/questions/11783932/…)
-
Edward Z. Yang almost 7 yearsI don't know why the comment above has two upvotes. See cmake.org/cmake/help/git-master/variable/… and cmake.org/cmake/help/git-master/variable/… : these flags are definitely in recent CMake versions.