6

I am trying to build a project in Microsoft Visual C++ 2013 disabling all non standard extensions.

#ifdef _MSC_VER
#include <windows.h>
#endif

In Configuration Properties --> C/C++ --> Language, I set "Disable Language Extensions" to yes (/Za).
However, building the previous code I get errors such as:

C:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h(11527): error C2467: illegal declaration of anonymous 'struct'

This means that the _MSC_VER macro is still defined, and "windows.h" has been included.

How can I include a file if and only if I am using Visual C++?

How can I set Visual C++ so that it compiles the code as Standard C++, marking all Microsoft extensions as errors?

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Pietro
  • 12,086
  • 26
  • 100
  • 193
  • 2
    as far as i know windows.h uses microsoft extensions – bolov Jul 10 '14 at 16:25
  • Maybe something like [this](http://stackoverflow.com/questions/9697013/how-to-undefine-msc-ver)? – Dan Shield Jul 10 '14 at 16:28
  • Go through your code base and make sure that you don't directly include any files that are not part of the standard library. – R Sahu Jul 10 '14 at 16:28
  • Seems to be a duplicate of http://stackoverflow.com/questions/5489326/za-compiler-directive-does-not-compile-system-headers-in-vs2010 – Christophe Jul 10 '14 at 16:31
  • 1
    windows.h uses heavily unnamed structs which are not allowed in standard C++ (see also: http://stackoverflow.com/questions/2253878/why-does-c-disallow-anonymous-structs-and-unions). A workaroud could be to give names to these structs. But you'd have to use these personal names to access the elements of the struct as well, which would change definitions compared to MS documentation. And if you would want to be able to compile your code with and without /Za, it would be a #define-#ifdef nightmare. – Christophe Jul 10 '14 at 17:20

1 Answers1

3

How can I include a file if and only if I am using Visual C++?

As you have already demonstrated, by checking _MSC_VER.

How can I set Visual C++ so that it compiles the code as Standard C++, marking all Microsoft extensions as errors?

You can't. I'm not aware of any compiler that allows this. Things like predefined macros for compiler version are entirely allowed by the standard, so they wouldn't be disabled as "nonstandard extensions."

If you want to check that your program builds for other platforms, then build your program on other platforms. GCC and Clang will tell you what they accept far better than Visual C++ will :)

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • So maybe the best thing to do is to define my own DISABLE_MSC_EXTENSIONS. – Pietro Jul 10 '14 at 16:44
  • 2
    @Pietro yes. And if you'd like to be portable, better structure your code so to isolate platform dependent code and use MS extensions only for these. – Christophe Jul 10 '14 at 17:23
  • What about this: _MSC_EXTENSIONS – Pietro Jul 11 '14 at 11:01
  • 1
    @pie: `_MSC_EXTENSIONS` is a [reserved symbol](https://en.cppreference.com/w/cpp/language/identifiers). You aren't allowed to use it, it is reserved for use by the implementation. – IInspectable Sep 16 '18 at 17:22