Turning on linker flags with CMake

2019-03-17 15:08发布

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?

5条回答
SAY GOODBYE
2楼-- · 2019-03-17 15:40

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)
查看更多
男人必须洒脱
3楼-- · 2019-03-17 15:41
我命由我不由天
4楼-- · 2019-03-17 15:47

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)

查看更多
Luminary・发光体
5楼-- · 2019-03-17 15:52

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

查看更多
做个烂人
6楼-- · 2019-03-17 16:06

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

查看更多
登录 后发表回答