22

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?

RBerteig
  • 41,948
  • 7
  • 88
  • 128
Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93

5 Answers5

31

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).

RBerteig
  • 41,948
  • 7
  • 88
  • 128
DMags
  • 1,621
  • 1
  • 13
  • 12
  • 2
    In recent CMake versions the only flag left is CMAKE_EXE_LINKER_FLAGS; the best thing to do is to edit the target properties (see http://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file) – koda Jun 09 '13 at 10:37
  • 3
    I don't know why the comment above has two upvotes. See https://cmake.org/cmake/help/git-master/variable/CMAKE_SHARED_LINKER_FLAGS.html?highlight=cmake_shared_linker_flags and https://cmake.org/cmake/help/git-master/variable/CMAKE_MODULE_LINKER_FLAGS.html?highlight=cmake_module_linker_flags : these flags are definitely in recent CMake versions. – Edward Z. Yang Jun 30 '17 at 14:54
4

and STATIC_LIBRARY_FLAGS http://www.cmake.org/cmake/help/v2.8.8/cmake.html#prop_tgt:STATIC_LIBRARY_FLAGS

for static libraries

elegant dice
  • 1,307
  • 12
  • 19
3

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).

onqtam
  • 4,356
  • 2
  • 28
  • 50
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)
DiB
  • 554
  • 5
  • 19
2

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)

kaveish
  • 1,296
  • 12
  • 21