Visual Studio: generator expression COMPILE_LANGUAGE is ignored for target_compile_options
Note that with Visual Studio Generators and Xcode there is no way to represent target-wide compile definitions or include directories separately for C and CXX languages. Also, with Visual Studio Generators there is no way to represent target-wide flags separately for C and CXX languages. Under these generators, expressions for both C and C++ sources will be evaluated using CXX if there are any C++ sources and otherwise using C. A workaround is to create separate libraries for each source file language instead:
But why? CMake knows about filetype of each file in build -- for example it marks .c
files with CompileAsC
. We can split COMPILE_OPTIONS
by unconditional (global section) and per file type if specified. Per language flags will be stored in additional options per file like
<ClCompile Include="R:\test_cmake\build\Test.cpp">
<ObjectFileName>$(IntDir)/Test.cpp.obj</ObjectFileName>
<AdditionalOptions>%(AdditionalOptions) /cppFlag</AdditionalOptions>
</ClCompile>
<ClCompile Include="R:\test_cmake\build\Test.c">
<ObjectFileName>$(IntDir)/Test.c.obj</ObjectFileName>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">CompileAsC</CompileAs>
<AdditionalOptions>%(AdditionalOptions) /cFlag</AdditionalOptions>
</ClCompile>
What do you think?