title: CMake: How can I compile defines and flags as string constants into my C(++) program?
body: <p>I'd like my C or C++ program that is built via CMake to be able to print (or otherwise make use of) the macro definitions and (other) C/C++ flags it was compiled with. So I want CMake to generate/configure a header or source file that defines respective strings constants and that is then built as part of/into my program.</p>
  <p>CMake features several commands (like <code>file()</code> or <code>execute_process()</code>) that would be executed when (respectively before) the build system is generated and thus would allow me to write such a source file, but I'm having trouble with getting the effective macro definitions and flags used for my target. E.g. there seem to be <code>COMPILE_DEFINITIONS</code> for the directory, the target, and for the configuration. Is there a way to get the macro definitions/C(++) flags that are effectively used for building my target? And how do I best write them into a source file?</p>
  <p>I've noticed, when using the Makefiles generator apparently a file &quot;${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MyTarget.dir/flags.make&quot; is created, which seems to contain pretty much what I'm looking for. So if there's no other way, I can probably make use of that file, but obviously that won't work for other generators and it comes with its own challenges (the file is generated after <code>execute_process()</code>).</p>