windows 是否可以检查您是否正在使用 Microsoft C 编译器为 64 位构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1647930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:21:35  来源:igfitidea点击:

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

cwindows64-bitvisual-c++

提问by dreamlax

Is there a simple preprocessor macro that is defined for a 64-bit build? I thought _WIN64might have been it, but even when I build a 32-bit target, the parts enclosed in a #ifdef _WIN64 ... #endifare 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 here. Maybe even something involving sizeof.

是否有为 64 位构建定义的简单预处理器宏?我想_WIN64可能是这样,但即使我构建了一个 32 位目标,包含在 a#ifdef _WIN64 ... #endif中的部分也会被编译,这会导致问题。今天是星期五,我无法直接思考,但我确定我在这里忽略了一些非常简单的事情。甚至可能涉及sizeof.

回答by Bruce Ikin

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

我一直使用 _WIN64 来检查它是否是 64 位版本。

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

注意 _WIN32 也总是(自动)由 MSVC 在 64 位版本中定义,因此在检查 _WIN32 之前检查 _WIN64:

#if defined( _WIN64 )

// Windows 64 bit code here

#elif defined( _WIN32 )

// Windows 32 bit code here

#else

// Non-Windows code here

#endif

回答by Michael Burr

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

听起来您的问题可能与不正确定义的标头或项目设置有关_WIN64- 这应该留给编译器。

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

WIN64_WIN64( 至少对于 Microsoft 编译器 - 其他编译器应该效仿,但并非所有人都这样做)之间存在细微差别:

  • _WIN64is defined by the compilerwhen 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)
  • WIN64is defined by the Windows Platform SDK(or whatever they're calling it this year) when targeting a 64-bit platform.
  • _WIN64编译器在为 Windows 64 位平台构建程序时定义。请注意,此名称位于编译器实现者的命名空间中(前导下划线后跟大写字母)
  • WIN64当面向 64 位平台时,由Windows 平台 SDK(或他们今年所称的任何名称)定义。

So if you're only including standard headers and don't take other measures to define it, WIN64will notbe defined.

所以如果你只包含标准头,不采取其他措施来定义它,WIN64不会被定义。

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

有一个类似的故事_WIN32and WIN32- 但检查其他编译器:WIN32即使只使用标准头文件,GCC 3.4.5 也确实定义了。数字火星也是如此。

Microsoft's compilers and Comeau do not.

Microsoft 的编译器和 Comeau 没有。

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

(希望)公知的琐事的另一个位是_WIN32WIN32靶向的64位的Windows平台时被设置。否则太多东西会坏掉。

回答by Ron Pihlgren

The Visual C++ compiler defines the following macros:

Visual C++ 编译器定义了以下宏:

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

回答by dirkgently

Check your project's build properties, particularly the preprocessor section. Are you defining _WIN64somewhere in WIN32builds? The sizeofthing probably won't work since you cannot use in a #iftest.

检查项目的构建属性,尤其是预处理器部分。您是否_WIN64WIN32构建中定义某个地方?这sizeof东西可能不起作用,因为你不能在#if测试中使用。