title: pass CMAKE_CXX_FLAGS in shell script
body: <p>I'm having a tedious issue involving bash and cmake and quoting, obviously.
  I need to compile a program multiple times, with different cmake flags. After a lot of guessing, I came up with the following structure in a bash script:</p>
  
  <pre><code>cmake_params[1]=-DCMAKE_CXX_FLAGS=-O2\ -DNDEBUG
  cmake_params[2]=-DCMAKE_CXX_FLAGS=-march=native\ -O2\ -DNDEBUG
  # ....
  
  #compile with:
  
  for i in {1..n}
  do
      # .. prepare ..
      mkdir build
      cd build
      cmake "${cmake_params[$i]}" ..
      make
  done
  </code></pre>
  
  <p>Note that I escaped whitespaces in the cmake_params and that cmake is called with double quotes. However, in the end, I need to be able to extend the flags to something as bad as:</p>
  
  <pre><code>cmake_params[1]=-DCMAKE_CXX_COMPILER="clang++"\
    -DCMAKE_C_COMPILER="clang"\
    -DCMAKE_CXX_FLAGS=-march=native\ -O3\ -DNDEBUG\
    -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=true\
    -DCMAKE_POLICY_DEFAULT_CMP0069=NEW
  </code></pre>
  
  <p>But that won't work. I tried quotes around the CXX flags, I tried omitting the quotes around the compiler and I tried omitting the quotes at the actual cmake call, all without success. The error messages vary, with the most prominent one being:</p>
  
  <pre><code>The CMAKE_CXX_COMPILER:
  
  clang++ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_FLAGS=-march=native -O3 -DNDEBUG
  -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=true -DCMAKE_POLICY_DEFAULT_CMP0069=NEW
  
  is not a full path and was not found in the PATH.
  </code></pre>
  
  <p>So obviously, cmake misinterprets my command line. To be extra clear, I want to pass clang++ as <code>CMAKE_CXX_COMPILER</code> and <code>-march=native -O3 -DNDEBUG</code> as <code>CMAKE_CXX_FLAGS</code>.</p>
  
  <p>Which bash script will do that and why - are there any resources on how quoting and parameter passing works? I literally tried one thing after the other to find something that works, but that was not even educated guesswork..</p>