how to undefine _MSC_VER?

六月ゝ 毕业季﹏ 提交于 2019-12-10 10:06:19

问题


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.


回答1:


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.




回答2:


_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.

http://msdn.microsoft.com/en-us/library/vstudio/b0084kay.aspx

You could either replace the condition ...

#if someOtherConditionGoesHere

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

#if defined(_MSC_VER) && someOtherConditionGoesHere



来源:https://stackoverflow.com/questions/9697013/how-to-undefine-msc-ver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!