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?
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.)
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.
and
and STATIC_LIBRARY_FLAGS http://www.cmake.org/cmake/help/v2.8.8/cmake.html#prop_tgt:STATIC_LIBRARY_FLAGS
for static libraries
You can add linker flags for a specific target using the
LINK_FLAGS
property:(note that I added a space before the flag since I'm using
APPEND_STRING
)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).
For adding linker flags - the following 4 CMake variables:
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).