MSVC19.27 / VS16.7 /std:c11 target_compile_features(... c_std_11)
Visual Studio 16.7 added the /std:c11
flag, which enables features such as Generic selection in the C11 standard.
It seems that for the test project below, CMake doesn't add this flag, so compile fails.
Example
We would like CMake compile line to include /std:c11
. This example uses Visual Studio 16.7.
CMake -> Ninja command line that fails is:
cl.exe /nologo /DWIN32 /D_WINDOWS /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\main.dir\main.c.obj /FdCMakeFiles\main.dir\ /FS -c ..\main.c
Meson -> Ninja command line (from PR referenced below) that works (notice the /std:c11) is:
"cl" "-Imain.exe.p" "-I." "-I.." "/MDd" "/nologo" "/showIncludes" "/W2" "/std:c11" "/ZI" "/Ob0" "/Od" "/RTC1" "/Fdmain.exe.p\main.c.pdb" /Fomain.exe.p/main.c.obj "/c" ../main.c
main.c
// https://www.ibm.com/support/knowledgecenter/en/SSGH2K_13.1.2/com.ibm.xlc131.aix.doc/language_ref/genericselection.html
#include <stdio.h>
#define myfunction(X) _Generic((X), \
long double:myfunction_longdouble, \
default:myfunction_double, \
float:myfunction_float \
)(X)
void myfunction_longdouble(long double x){printf("calling %s\n",__func__);}
void myfunction_double(double x){printf("calling %s\n",__func__);}
void myfunction_float(float x){printf("calling %s\n",__func__);}
int main()
{
long double ld;
double d;
float f;
myfunction(ld);
myfunction(d);
myfunction(f);
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.12) # I also tried minimum 3.18
project(msvc_c11 LANGUAGES C)
add_executable(main main.c)
target_compile_features(main PRIVATE c_std_11)