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.
Lekensteyn Feb 12 '16 at 11:09 2016-02-12 11:09
source share