Search code examples
windowsclangcompatibility

Clang on Windows: how to disable the default MSVC compatibility?


From Clang's documentation:

When Clang compiles C++ code for Windows, it attempts to be compatible with MSVC.

In particular, Clang defines _MSC_VER.

MSVC does not support #pragma STDC FENV_ACCESS ON:

cl t125.c /std:c11
t125.c(11): warning C4068: unknown pragma 'STDC'

Instead MSVC does support #pragma fenv_access (on). This leads to this code:

#if _MSC_VER
#pragma fenv_access (on)
#else
#pragma STDC FENV_ACCESS ON
#endif

Now try to compile this code using the latest Clang on Windows:

$ /cygdrive/d/SOFTWARE/LLVM/12.0.0/bin/clang t125.c -Wall -Wextra
t125.c:10:9: warning: unknown pragma ignored [-Wunknown-pragmas]
#pragma fenv_access (on)

Meaning that since #pragma fenv_access (on) is unknown pragma which is ignored, then the MSVC compatibility is underdone / unfinished? Confused.

Question: how to disable the default MSVC compatibility (so that Clang does not define _MSC_VER)?

P.S. This finally leads to this code:

#if _MSC_VER && ! __clang__ && ! __INTEL_COMPILER 
#pragma fenv_access (on)    /* true MSVC here?? */
#else
#pragma STDC FENV_ACCESS ON
#endif

UPD. Patch for supporting the MS spelling of the pragma: https://reviews.llvm.org/D111440.


Solution

  • how to disable the default MSVC compatibility (so that Clang does not define _MSC_VER)?

    Adding either -fms-compatibility-version=0 or -fmsc-version=0 switch should prevent _MSC_VER from being defined. This and a few other related VC++ compatibility options are documented on the Clang command line argument reference page under Target-independent compilation options.

    -fms-compatibility, -fno-ms-compatibility
    Enable full Microsoft Visual C++ compatibility

    -fms-compatibility-version=<arg>
    Dot-separated value representing the Microsoft compiler version number to report in _MSC_VER (0 = don’t define it (default))

    -fms-extensions, -fno-ms-extensions
    Accept some non-standard constructs supported by the Microsoft compiler

    -fms-memptr-rep=<arg>

    -fms-volatile

    -fmsc-version=<arg>
    Microsoft compiler version number to report in _MSC_VER (0 = don’t define it (default))