ClangCL: VS generator sets CXX standard for C files when a target contains both C and CXX files
I am building a project using the clang-cl compiler and the Visual Studio generator. This project has both C files and CXX files. I want to specify the language standard for the CXX files to be c++17, but that adds the flag to all files in the target, even C files. clang-cl warns about the unused commandline flag, and fails when warnings as errors is enabled.
Here's a CMakeLists.txt to reproduce the issue:
project(cmake_vs_c_file_test C CXX)
cmake_minimum_required(VERSION 3.15)
add_library(main STATIC main.c foo.cpp)
target_compile_features(main PRIVATE cxx_std_17)
target_compile_options(main PRIVATE
$<IF:$<CXX_COMPILER_ID:MSVC>,/WX,-Werror>
)
The actual contents of main.c
and foo.cpp
don't matter. I can reproduce the issue with just empty files.
I'm using the Clang toolchain provided by Visual Studio 2019. I'm generating the solution with cmake.exe .. -G "Visual Studio 16 2019" -A x64 -T ClangCL
.
At build time, I get a build command for main.c
that still has the /std:c++17
present, which causes clang-cl to error because -Wunused-command-line-argument
is on by default, and I enabled warnings as errors:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\x64\bin\clang-cl.exe /c /Z7 /nologo /W3 /WX- /diagnostics:column /Od /Ob0 /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS
/EHsc /MDd /GS /fp:precise /GR /std:c++17 /Fo"main.dir\Debug\\" /Gd /TC -m64 E:\ws\cmake_vs_c_file_test\main.c
clang-cl : error : argument unused during compilation: '/std:c++17' [-Werror,-Wunused-command-line-argument] [E:\ws\cmake_vs_c_file_test\build\main.vcxproj]