title: CMake clang and c++0x
body: <p>When using clang++, how can I make CMake use the <code>-std=c++0x</code> flag when compiling, but not when linking?</p>
  
  <p>There are several other posts regarding using clang as compiler, but I have not found any hints on setting the c++ standard.</p>
  
  <p>Here is what I tried:</p>
  
  <p>CMakeLists.txt:</p>
  
  <pre><code>project(test)
  add_executable(main main.cxx)
  </code></pre>
  
  <p>ClangOverride.txt:</p>
  
  <pre><code>SET (CMAKE_C_FLAGS_INIT                "-Wall -std=c99")
  SET (CMAKE_C_FLAGS_DEBUG_INIT          "-g")
  SET (CMAKE_C_FLAGS_MINSIZEREL_INIT     "-Os -DNDEBUG")
  SET (CMAKE_C_FLAGS_RELEASE_INIT        "-O3 -DNDEBUG")
  SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
  
  SET (CMAKE_CXX_FLAGS_INIT                "-Wall -std=c++0x -stdlib=libc++")
  SET (CMAKE_CXX_FLAGS_DEBUG_INIT          "-g")
  SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT     "-Os -DNDEBUG")
  SET (CMAKE_CXX_FLAGS_RELEASE_INIT        "-O3 -DNDEBUG")
  SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
  SET (CMAKE_EXE_LINKER_FLAGS_INIT         "")
  </code></pre>
  
  <p>main.cxx:</p>
  
  <pre><code>int main(){ return 0; }
  </code></pre>
  
  <p>Command used to invoke <code>cmake</code></p>
  
  <pre><code>CC=clang CXX=clang++ cmake .. -DCMAKE_USER_MAKE_RULES_OVERRIDE=ClangOverride.txt -DCMAKE_BUILD_TYPE=Release
  </code></pre>
  
  <p>Building the project:</p>
  
  <pre><code>VERBOSE=1 make
  </code></pre>
  
  <p>This will invoke the following two commands:</p>
  
  <pre><code>/usr/bin/clang++    -Wall -std=c++0x -stdlib=libc++ -O3 -DNDEBUG   -o CMakeFiles/main.dir/main.cxx.o -c /tmp/asdf/main.cxx
  /usr/bin/clang++    -Wall -std=c++0x -stdlib=libc++ -O3 -DNDEBUG    CMakeFiles/main.dir/main.cxx.o  -o main -rdynamic
  </code></pre>
  
  <p>The second command results in warning because if the un-used flag: -std=c++0x</p>
  
  <pre><code>clang: warning: argument unused during compilation: '-std=c++0x'
  CMakeFiles/main.dir/main.cxx.o: file not recognized: File format not recognized
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>How can I avoid this?</p>