How to Add a Linker or Compile Flag in a Cmake File

How do I add a linker or compile flag in a CMake file?

Note: Given CMake evolution since this was answer was written in 2012, most of the suggestions here are now outdated/deprecated and have better alternatives.


Suppose you want to add those flags (better to declare them in a constant):

SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
SET(GCC_COVERAGE_LINK_FLAGS "-lgcov")

There are several ways to add them:

  1. The easiest one (not clean, but easy and convenient, and works only for compile flags, C & C++ at once):

     add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
  2. Appending to corresponding CMake variables:

     SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
  3. Using target properties, cf. doc CMake compile flag target property and need to know the target name.

     get_target_property(TEMP ${THE_TARGET} COMPILE_FLAGS)
    if(TEMP STREQUAL "TEMP-NOTFOUND")
    SET(TEMP "") # Set to empty string
    else()
    SET(TEMP "${TEMP} ") # A space to cleanly separate from existing content
    endif()
    # Append our values
    SET(TEMP "${TEMP}${GCC_COVERAGE_COMPILE_FLAGS}" )
    set_target_properties(${THE_TARGET} PROPERTIES COMPILE_FLAGS ${TEMP} )

Right now I use method 2.

Adding compiler flags to CMakeLists.txt

CMake has native support for all the things you're trying to solve by explicit flag specification, so you should use that instead:

cmake_minimum_required (VERSION 3.0)
project (foo)

add_executable (first first.cpp)
target_compile_options (first PRIVATE -std=c++11)
target_compile_definitions (first PRIVATE BOOST_ERROR_CODE_HEADER_ONLY)
target_link_libraries (first pthread)

If you can increase your minimum CMake version requirement to 3.1, you can replace the hard-coded std flag with native CMake too:

cmake_minimum_required (VERSION 3.1)
project (foo)

add_executable (first first.cpp)
set_target_properties (first PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED TRUE
CXX_EXTENSIONS FALSE
)
target_compile_definitions (first PRIVATE BOOST_ERROR_CODE_HEADER_ONLY)
target_link_libraries (first pthread)

The advatange is that not all compilers express "use C++11 without extensions" the same way, and CMake will translate the requirement to the correct flags for you.

If you're going to need the same C++11 setup for many executables, you can instead set CMake variables which prepopulate the properties. Note that the variables must be set before the executables are created:

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_EXTENSIONS FALSE)

add_executable (first first.cpp)
target_compile_definitions (first PRIVATE BOOST_ERROR_CODE_HEADER_ONLY)
target_link_libraries (first pthread)

add_executable (second second.cpp)
target_compile_definitions (second PRIVATE BOOST_ERROR_CODE_HEADER_ONLY)
target_link_libraries (second pthread)

# ... and so on

setting compiler/linker flags per target in CMake [duplicate]

Ok, so after browsing through different answers here I've made the CMakelists.txt given below and it works in this case.

cmake_minimum_required(VERSION 3.0)

project(ABC_PROXY VERSION 1.0.0 LANGUAGES C CXX)

add_executable(abc_proxy_with_asan
src/file1.c
src/main.cpp
)

set_target_properties(abc_proxy_with_asan PROPERTIES COMPILE_FLAGS "-g3 -fsanitize=address -fno-omit-frame-pointer")
set_target_properties(abc_proxy_with_asan PROPERTIES LINK_FLAGS "-fsanitize=address -static-libasan")
target_include_directories(abc_proxy_with_asan PRIVATE /home/vishal/cpp_file/new /home/vishal/cpp_file/new/framework)

add_executable(abc_proxy
src/file1.c
src/main.cpp
)

set_target_properties(abc_proxy PROPERTIES COMPILE_FLAGS "-g3 -fno-omit-frame-pointer")
target_include_directories(abc_proxy PRIVATE /home/vishal/cpp_file/new /home/vishal/cpp_file/new/framework)

How can I add linker flag for libraries with CMake?

Note: modern CMake has a better solution than mentioned below (see updates for details).

You can use CMAKE_SHARED_LINKER_FLAGS like:

set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")

This question looks like related.

UPD

Thanks to @Bruce Adams who points out that since v3.13 CMake has special command for such purpose: add_link_options.

UPD 2

Thanks to @Alex Reinking who points out that modern CMake doesn't recommend using global settings. It is suggested to give the preference to the property settings before the global ones, so instead of add_link_options that has a global scope, the target_link_options should be used. See Alex's answer for details.

How to add -l (ell) compiler flag in CMake

Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:

target_link_libraries(day_g pq)

CMake set per configuration linker flag

According to https://cmake.org/cmake/help/latest/prop_tgt/STATIC_LIBRARY_OPTIONS.html STATIC_LIBRARY_OPTIONS property does the trick

set_property(TARGET Runtime PROPERTY STATIC_LIBRARY_OPTIONS ${LIB_LINKS_RUNTIME})

Adding global compile flags in CMake

Conclusion: add_compile_options and add_definitions work on the current directory and all included directories that are included after the command. Setting CMAKE_CXX_FLAGS, however, seems to only work on the current directory. Not sure why however, because as commenter Tsyvarev says, it should have the same scope as the first two methods.

Basically, shifting the lines around like this:

[...]
# Custom commands
add_compile_options ( -Wno-reorder )
add_definitions ( -Wno-unknown-pragmas )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")

add_subdirectory(0_dummy)
add_subdirectory(1_cannonball)
add_subdirectory(2_spring)
add_subdirectory(3_spinning)
add_subdirectory(4_gyro)

I no longer get -Wreorder and -Wunknown-pragmas warnings, but I still get -Wsign-compare warnings.



Related Topics



Leave a reply



Submit