How to extract compile flags from setting CMAKE_CXX_STANDARD

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

How to extract compile flags from setting CMAKE_CXX_STANDARD

"Benedikt Führer"
I'm using the following function to setup precompiled headers for unix. The goal is to extract all flags and definitions from the current target and add them to the command line for compiling the precompiled header.
function(export_all_flags _filename)
  set(_include_directories "$<TARGET_PROPERTY:${_target},INCLUDE_DIRECTORIES>")
  set(_compile_definitions "$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")
  set(_compile_flags "$<TARGET_PROPERTY:${_target},COMPILE_FLAGS>")
  set(_compile_options "$<TARGET_PROPERTY:${_target},COMPILE_OPTIONS>")
  set(_include_directories "$<$<BOOL:${_include_directories}>:-I$<JOIN:${_include_directories},\n-I>\n>")
  set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-D>\n>")
  set(_compile_flags "$<$<BOOL:${_compile_flags}>:$<JOIN:${_compile_flags},\n>\n>")
  set(_compile_options "$<$<BOOL:${_compile_options}>:$<JOIN:${_compile_options},\n>\n>")
  file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}\n")
The problem is, I set CMAKE_CXX_STANDARD to sth above 11 which will result in -std=gnu++11 being set in any of my target's compilation units. However the appropriate flag isn't captured by the above function. It seems like the flag is added after the function runs (I call it after add_executable or add_library) or not being visible to query on the target at all. What's an appropriate solution here?


Powered by

Please keep messages on-topic and check the CMake FAQ at:

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support:
CMake Consulting:
CMake Training Courses:

Visit other Kitware open-source projects at

Follow this link to subscribe/unsubscribe: