how to undefine _MSC_VER?
Asked Answered
V

2

3

I work in Visual Studio but my project is for a POSIX-based environment (marmalade sdk). In this project, the release build is compiled with gcc for ARM but the debug version works on windows and is compiled by MS compiler. Also this environmet has its own implementation of STL and other standard libraries.

Many of these c++ librares have code like this:

#if defined( _MSC_VER )
   #include <Windows.h>
#else
   #include <pthread.h>
#endif

Is it possible to undefine the _MSC_VER macro? - So that the C++ libraries will detect a POSIX system here.

Verdi answered 14/3, 2012 at 6:34 Comment(4)
_MSC_VER is only defined when compiling with the Visual Studio C++ compiler. If you use any other compiler it will not be defined.Chapland
Yes, it compiling with the Visual Studio C++ compiler but enviroument not use any windows librares like Windows.h - it's don'have it. My qestion about how to tell to compiler that is not standart windows environment.Verdi
you might be better served using a build based define like _WIN32, the _MSC_VER is mostly likely going to be impossible to undefine as its a compiler builtinKultur
_MSC_VER is intended to show which compiler is used. It says nothing about the build environment. So you should just use a different mecroPycno
T
3

Of course:

#undef _MSC_VER

#if defined( _MSC_VER )
   #include <Windows.h>
#else
   #include <pthread.h>
#endif

Or, #undef it before you include the file where _MSC_VER is used.

Transcendentalistic answered 14/3, 2012 at 6:39 Comment(2)
Thanks. I tried to undefine it at global project level but this not produce a result.Verdi
@strobe: if abc.h file contains #if defined( _MSC_VER ) and abc.cpp #includes abc.h, then please try #undef _MSC_VER and then #include abc.h.Transcendentalistic
E
6

_MSC_VER is (and always should be) defined when compiling with the Microsoft compiler so that it "evaluates to the major and minor number components of the compiler's version number". Therefore, the code is using the wrong macro test, since it will always be defined to some value for your compiler regardless of the Windows environment differences.

Rather than destroy the definition of _MSC_VER (which could lead to other problems if any code really does want to know the compiler version), what you really should do instead is to correct the condition so that a more appropriate macro test is used that distinguishes between the kinds of Windows environments that you might encounter.

See the more complete list of predefined macros you could consider here

You could either replace the condition ...

#if someOtherConditionGoesHere

... or extend it with additional conditions, e.g.

#if defined(_MSC_VER) && someOtherConditionGoesHere
Exothermic answered 22/4, 2013 at 20:9 Comment(0)
T
3

Of course:

#undef _MSC_VER

#if defined( _MSC_VER )
   #include <Windows.h>
#else
   #include <pthread.h>
#endif

Or, #undef it before you include the file where _MSC_VER is used.

Transcendentalistic answered 14/3, 2012 at 6:39 Comment(2)
Thanks. I tried to undefine it at global project level but this not produce a result.Verdi
@strobe: if abc.h file contains #if defined( _MSC_VER ) and abc.cpp #includes abc.h, then please try #undef _MSC_VER and then #include abc.h.Transcendentalistic

© 2022 - 2024 — McMap. All rights reserved.