Turning on linker flags with CMake

后端 未结 5 1228
萌比男神i
萌比男神i 2020-12-30 00:37

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)

相关标签:
5条回答
  • 2020-12-30 01:10

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

    for static libraries

    0 讨论(0)
  • 2020-12-30 01:17

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

    0 讨论(0)
  • 2020-12-30 01:22

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

    0 讨论(0)
  • 2020-12-30 01:23

    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)
    
    0 讨论(0)
  • 2020-12-30 01:30

    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)

    0 讨论(0)
提交回复
热议问题