CMake generator expression, differentiate C / C ++ code

I would like to add -std=c++11 to my

 add_compile_options("-std=c++11") 

However, this also adds them to the compilation of C files, not just C ++. I know that I can add conditional flag compilations depending on the configuration used:

 add_compile_options("$<$<CONFIG:DEBUG>:-addMeInDebugOnly>") 

How can I add my flag only to C ++ files? I am looking for something like:

 add_compile_options("$<$<??:??>:-std=c++11>") 

But what do I need to fill out with question marks?

+8
c ++ c c ++ 11 cmake
Aug 27 '14 at 10:41
source share
3 answers

You can use the LINKER_LANGUAGE target property to add a flag only to C ++ * objects:

 add_compile_options( "$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-std=c++11>" ) 

* Note that this will not work for targets with mixed C / C ++ sources

CMAKE_CXX_FLAGS should work fine:

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 

You probably need to add them to the cache if it is installed before the project command (for example, in the toolchain):

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" CACHE STRING "" FORCE) 
+2
Aug 30 '14 at 8:32
source share
— -

If you have mixed C and C ++ sources, the LINKER_LANGUAGE property may use the wrong flags to compile individual sources. The solution is to use the COMPILE_LANGUAGE generator COMPILE_LANGUAGE (introduced with CMake 3.3). The simplest example for your original C ++ 1x flag:

 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=c++11>) 

If you have a string of compilation options (for example, for use with the target property COMPILE_FLAGS ), you need to separate the flags

 set(WARNCFLAGS "-Wall -Wextra -Wfuzzle -Wbar") # ... string(REPLACE " " ";" c_flags "${WARNCFLAGS}") string(REPLACE " " ";" cxx_flags "${WARNCXXFLAGS} ${CXX1XCXXFLAGS}") add_compile_options( "$<$<COMPILE_LANGUAGE:C>:${c_flags}>" "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>" ) # Two alternative variants for single targets that take strings: target_compile_options(some-target PRIVATE "${WARNCFLAGS}") set_target_properties(some-target PROPERTIES COMPILE_FLAGS "${WARNCFLAGS}") 

The use of strings, however, is deprecated in favor of lists. When lists are used, you can use:

 set(c_flags -Wall -Wextra -Wfuzzle -Wbar) # ... add_compile_options( "$<$<COMPILE_LANGUAGE:C>:${c_flags}>" "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>" ) # Two alternative variants for single targets given a list: target_compile_options(some-target PRIVATE ${f_flags}) set_target_properties(some-target PROPERTIES COMPILE_OPTIONS "${c_flags}") 

Pay attention to the citation. If the list is not quoted, it expands to its elements (and is no longer a list). To pass a list between commands, quote it.

+13
Feb 12 '16 at 11:09
source share

I would rather do it like this:

 set_source_files_properties( ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp PROPERTIES COMPILE_FLAGS "-std=c++11") 

where the documentation for set_source_files_properties is at http://www.cmake.org/cmake/help/v3.0/command/set_source_files_properties.html

+3
Aug 27 '14 at 10:44 on
source share



All Articles