title: cmake - Global linker flag setting (for all targets in directory)
body: <p>I want to pass linker flags to all sub-projects (sub-directory CMakeList) in my project.</p>
  
  <p>Before switching to new cmake 3.3, I was using the following code (cmake 3.2) which was working well, adding flags for both compilation and linking :</p>
  
  <pre><code>set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -stdlibc++")
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -stdlibc++")
  </code></pre>
  
  <p>With cmake 3.3 this no longer works and set the flags only for compilation step. I updated the CMakeList to use a more "modern" cmake syntax :</p>
  
  <pre><code>set(MY_DEBUG_OPTIONS -g -stdlib=libstdc++ -Wfatal-errors)
  set(MY_RELEASE_OPTIONS -O3 -stdlib=libstdc++ -Wfatal-errors)
  
  add_compile_options(
    "$&lt;$&lt;CONFIG:DEBUG&gt;:${MY_DEBUG_OPTIONS}&gt;"
    "$&lt;$&lt;CONFIG:RELEASE&gt;:${MY_RELEASE_OPTIONS}&gt;")
  </code></pre>
  
  <p>This set compilation flags for all sub-projects, is there a similar way of doing this for linker flags ? I know one can add linker flags on a target basis with <code>target_link_libraries</code>command but can't find anything else.</p>
  
  <p>I tried using <code>CMAKE_SHARED_LINKER_FLAGS</code> (and corresponding var for exe, module,..) variable with no success.</p>
  
  <p><strong>Update :</strong></p>
  
  <p>It turns out that this has nothing to do with cmake version, things work correctly with <code>CMAKE_CXX_FLAGS_XXX</code>variables, except on first make command. If one run <code>make</code> a second time (with a modification in CmakeList), flags are presents.</p>
  
  <p>I think I found a solution while testing with a simple CMakeList : if flags are declared after the <code>project</code> command it just work as expected. I don't know if it's a requirement from cmake itself or just a weird behavior.</p>
  
  <pre><code>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})
  </code></pre>
  
  <p>Using :</p>
  
  <pre><code>cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release .
  </code></pre>