C++中定义的WIN32和_WIN32有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/662084/
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-08-27 16:34:01  来源:igfitidea点击:

What's the difference between the WIN32 and _WIN32 defines in C++

c++c-preprocessor

提问by Adam Naylor

I know that WIN32denotes win32 compilation but what is _WIN32used for?

我知道这WIN32表示 win32 编译但_WIN32用于什么?

采纳答案by Adam Naylor

WIN32is a name that you could use and even define in your own code and so might clash with Microsoft's usage. _WIN32is a name that is reserved for the implementor (in this case Microsoft) because it begins with an underscore and an uppercase letter - you are not allowed to define reserved names in your own code, so there can be no clash.

WIN32是一个您可以在自己的代码中使用甚至定义的名称,因此可能与 Microsoft 的用法发生冲突。_WIN32是为实现者(在本例中为 Microsoft)保留的名称,因为它以下划线和大写字母开头 - 不允许您在自己的代码中定义保留名称,因此不会发生冲突。

回答by Michael Burr

To elaborate (Neil Butterworth and blue.tuxedo have already given the correct answer):

详细说明(Neil Butterworth 和 blue.tuxedo 已经给出了正确答案):

  • WIN32is defined by the SDK or the build environment, so it does not use the implementation reserved namespace
  • _WIN32is defined by the compilerso it uses the underscore to place it in the implementation-reserved namespace
  • WIN32由 SDK 或构建环境定义,因此不使用实现保留的命名空间
  • _WIN32编译器定义,因此它使用下划线将其放置在实现保留的命名空间中

You'll find a similar set of dual defines with nearly identical names and similar uses such as _UNICODE/UNICODE, _DEBUG/DEBUG, or maybe _DLL/DLL(I think that only the UNICODE ones get much of any use in their different versions). Though sometimes in these cases (like _UNICODE), instead of the underscore version being defined bythe compiler, they are used to control what the CRT headers do:

您会发现一组类似的双重定义,它们具有几乎相同的名称和类似的用法,例如_UNICODE/ UNICODE_DEBUG/DEBUG_DLL/ DLL(我认为只有 UNICODE 的定义在它们的不同版本中有很多用途)。尽管有时在这些情况下(如_UNICODE),而不是编译器定义的下划线版本,它们用于控制 CRT 标头的功能:

  • _UNICODEtells the CRT headers that CRT names which can be either Unicode or ANSI (such as _tcslen()should map to the wide character variant (wcslen())
  • UNICODEdoes something similar for the SDK (maps Win32 APIs to their "W" variants)
  • _UNICODE告诉 CRT 标头 CRT 名称可以是 Unicode 或 ANSI(例如_tcslen()应该映射到宽字符变体 ( wcslen())
  • UNICODE为 SDK 做一些类似的事情(将 Win32 API 映射到它们的“ W”变体)

Essentially the versions with the underscore are controlled by or used by the compiler team, the versions without the underscore are controlled/used by teams outside of the compiler. Of course, there's probably going to be a lot overlap due to compatibility with past versions and just general mistakes by one team or the other.

本质上,带下划线的版本由编译器团队控制或使用,没有下划线的版本由编译器外部的团队控制/使用。当然,由于与过去版本的兼容性以及一个团队或另一个团队的一般错误,可能会有很多重叠。

I find it confusing as hell - and find that they are used nearly interchangeably in user code (usually, when you see one defined, you'll see the other defined in the same place, because if you need one you need the other). Personally, I think that you should usethe versions without the underscore (unless you're writing the compiler's runtime) and make sure they both get defined (whether via hearers or compiler switches as appropriate) when you're defining one.

我觉得这很令人困惑 - 并且发现它们在用户代码中几乎可以互换使用(通常,当您看到一个已定义时,您会在同一位置看到另一个定义,因为如果您需要一个,则需要另一个)。就个人而言,我认为您应该使用不带下划线的版本(除非您正在编写编译器的运行时),并确保在您定义一个时,它们都得到定义(无论是通过听者还是适当的编译器开关)。



Note that the SDK will define _WIN32when building for the Mac because the compiler doesn't, kind of overstepping it's bounds. I'm not sure what projects use a Win32 API an a compiler targeting the Mac - maybe some version of Office for the Max or something.

请注意,SDK 将_WIN32在为 Mac 构建时进行定义,因为编译器没有,有点超出了它的界限。我不确定哪些项目使用 Win32 API 和针对 Mac 的编译器 - 也许某些版本的 Office for the Max 或其他东西。

回答by bltxd

WIN32 is a user-defined flag which may be required by some headers. _WIN32 is automatically defined by the visual C/C++ compiler. Since it begins with an _ followed by a capital character, it is reserved by the implementation (meaning the C/C++ toolchain provider).

WIN32 是用户定义的标志,某些标头可能需要它。_WIN32 由 Visual C/C++ 编译器自动定义。由于它以 _ 开头,后跟大写字符,因此由实现保留(即 C/C++ 工具链提供程序)。

I prefer to use (read) _WIN32, seems safer to me.

我更喜欢使用(读取)_WIN32,对我来说似乎更安全。