开发者

Is it possible to check whether you are building for 64-bit with Microsoft C Compiler?

开发者 https://www.devze.com 2022-12-10 00:20 出处:网络
Is there a simple preprocessor macro that is defined for a 64-bit build? I thought _WIN64 might have been it, but even when I build a 32-bit target, the parts enclosed in a #ifdef _WIN64 ... #endif ar

Is there a simple preprocessor macro that is defined for a 64-bit build? I thought _WIN64 might have been it, but even when I build a 32-bit target, the parts enclosed in a #ifdef _WIN64 ... #endif are compiled in, and this is causing problems. It's Friday and I can't think straight, but I'm sure I'm overlooking something very simple her开发者_运维百科e. Maybe even something involving sizeof.


I have always used _WIN64 to check if it is a 64 bit build.

N.B. _WIN32 is also always (automatically) defined by MSVC in 64 bit builds, so check for _WIN64 before you check for _WIN32:

#if defined( _WIN64 )

// Windows 64 bit code here

#elif defined( _WIN32 )

// Windows 32 bit code here

#else

// Non-Windows code here

#endif


It sounds like your problem might be related to a header or project setting improperly defining _WIN64 - that should be left to the compiler.

There's a subtle difference between WIN64 and _WIN64 (at least for the Microsoft compilers - other compilers should follow suit, but not all do):

  • _WIN64 is defined by the compiler when it's building a program for a Windows 64-bit platform. Note that this name is in the compiler implementor's namespace (leading underscore followed by a capital letter)
  • WIN64 is defined by the Windows Platform SDK (or whatever they're calling it this year) when targeting a 64-bit platform.

So if you're only including standard headers and don't take other measures to define it, WIN64 will not be defined.

There's a similar story for _WIN32 and WIN32 - but checking other compilers: GCC 3.4.5 does define WIN32 even if only standard headers are used. As does Digital Mars.

Microsoft's compilers and Comeau do not.

Another bit of (hopefully) well known trivia is that _WIN32 and WIN32 are set when targeting 64-bit Windows platforms. Too much stuff would have broken otherwise.


The Visual C++ compiler defines the following macros:

  • _M_IX86 - x86 platform
  • _M_IA64 - ia64 platform
  • _M_X64 - x64 platform


Check your project's build properties, particularly the preprocessor section. Are you defining _WIN64 somewhere in WIN32 builds? The sizeof thing probably won't work since you cannot use in a #if test.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号