I want to pass linker flags to all subprojects (CMakeList subdirectory) in my project.
Prior to upgrading to the new cmake 3.3, I used the following code (cmake 3.2) that worked well, adding flags for both compilation and linking:
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -stdlibc++") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -stdlibc++")
With cmake 3.3, this no longer works and sets flags only for the compilation phase. I updated CMakeList to use the more "modern" cmake syntax:
set(MY_DEBUG_OPTIONS -g -stdlib=libstdc++ -Wfatal-errors) set(MY_RELEASE_OPTIONS -O3 -stdlib=libstdc++ -Wfatal-errors) add_compile_options( "$<$<CONFIG:DEBUG>:${MY_DEBUG_OPTIONS}>" "$<$<CONFIG:RELEASE>:${MY_RELEASE_OPTIONS}>")
This set of compilation flags for all subprojects, is there a similar way to do this for linker flags? I know that you can add linker flags on a target basis with the target_link_libraries , but cannot find anything.
I tried using the variable CMAKE_SHARED_LINKER_FLAGS (and the corresponding variable for exe, module, ..) without success.
Update:
It turns out that this has nothing to do with the cmake version, everything works correctly with the CMAKE_CXX_FLAGS_XXX variables, except for the first make command. If you run make second time (with a modification in CmakeList), the flags will be presented.
I think I found a solution when testing with a simple CMakeList: if flags are declared after the project command, it works as expected. I donβt know if this is a requirement from cmake itself or just weird behavior.
cmake_minimum_required (VERSION 3.2) set(PROJECT Test_Project) # Not working (on first run) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -stdlib=libstdc++ -Wfatal-errors") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -stdlib=libstdc++ -Wfatal-errors") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -stdlib=libstdc++ -Wfatal-errors") project(${PROJECT}) # Declare here instead... add_executable(Test test.cpp) MESSAGE( STATUS "Config flags : " ${CMAKE_CXX_FLAGS_RELEASE})
Using:
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release .
c ++ linker cmake
rma Sep 21 '15 at 15:05 2015-09-21 15:05
source share