title: How to print current compilation flags that are set with target_compile_options()?
body: <p>I am trying to print compilation flags that are set for target. The best scenario is to print a line with current flags on configure and compilation times, but if it's impossible, then on configure time only (or compilation only) (acceptable solution). </p>
  
  <p>This is my testing .c file:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  int main() {
      printf("Hello, World!\n");
      return 0;
  }
  </code></pre>
  
  <p>And CMakeLists.txt:</p>
  
  <pre class="lang-c prettyprint-override"><code>cmake_minimum_required(VERSION 3.10)
  project(cmake_gcc_options_try_c C)
  
  set(CMAKE_C_STANDARD 11)
  set(CMAKE_C_STANDARD_REQUIRED ON)
  
  add_executable(cmake_gcc_options_try_c main.c)
  
  target_compile_options(cmake_gcc_options_try_c 
                         PUBLIC -W -Wall -Wextra -pedantic -pedantic-errors)
  
  # This fails
  message("-- Current compiler flags CMAKE_C_FLAGS are: ${CMAKE_C_FLAGS}")
  message("-- Current compiler flags C_FLAGS are: ${C_FLAGS}")
  </code></pre>
  
  <p>and</p>
  
  <pre class="lang-bash prettyprint-override"><code>cmake . &amp;&amp; make
  </code></pre>
  
  <p>Gives this output:</p>
  
  <pre class="lang-none prettyprint-override"><code>-- The C compiler identification is GNU 7.4.0
  -- Check for working C compiler: /usr/bin/cc
  -- Check for working C compiler: /usr/bin/cc -- works
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Detecting C compile features
  -- Detecting C compile features - done
  -- Current compiler flags CMAKE_C_FLAGS are: 
  -- Current compiler flags C_FLAGS are: 
  -- Configuring done
  -- Generating done
  -- Build files have been written to: /home/user/Projects/cmake-gcc-options-try-c
  Scanning dependencies of target cmake_gcc_options_try_c
  [ 50%] Building C object CMakeFiles/cmake_gcc_options_try_c.dir/main.c.o
  [100%] Linking C executable cmake_gcc_options_try_c
  [100%] Built target cmake_gcc_options_try_c
  </code></pre>
  
  <p>Why <code>CMAKE_C_FLAGS</code> and <code>C_FLAGS</code> are printed as they were undefined?</p>
  
  <p>How to achieve this print on <code>make</code> command:</p>
  
  <pre class="lang-none prettyprint-override"><code>[ 50%] Building C object CMakeFiles/cmake_gcc_options_try_c.dir/main.c.o
  [ 50%] Current compiler flags are: -W -Wall -Wextra -pedantic -pedantic-errors -std=gnu11
  [100%] Linking C executable cmake_gcc_options_try_c
  [100%] Built target cmake_gcc_options_try_c
  </code></pre>
  
  <p>?</p>
  
  <p><strong>Update:</strong> <a href="https://stackoverflow.com/users/207791/victor-sergienko">Viktor Sergienko</a> came with a working solution, but one problem with this it's not so pretty print:
  <a href="https://i.stack.imgur.com/Tj4Ej.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Tj4Ej.png" alt="enter image description here"></a>
  Any thoughts how to make it to be in format of other prints? E. g. :</p>
  
  <pre><code>[ 50%] Building C object CMakeFiles/cmake_gcc_options_try_c.dir/main.c.o
  [100%] Linking C executable cmake_gcc_options_try_c
  [100%] Current compiler flags are: -W -Wall -Wextra -pedantic -pedantic-errors -std=gnu11
  [100%] Built target cmake_gcc_options_try_c
  </code></pre>
  
  <p>Second problem is that <code>-std=gnu11</code> is not printed (but it is enabled with <code>set(CMAKE_C_STANDARD 11)</code> and <code>set(CMAKE_C_STANDARD_REQUIRED ON)</code>)</p>