WIN32和_WIN32在c++中定義的區別是什么

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


I know that WIN32 is obviously to denote win32 compilation but what is the need for _WIN32?

我知道WIN32顯然是用來表示WIN32編譯的,但是_WIN32有什么用呢?

3 个解决方案

#1


44  

WIN32 is a name that you could use and even define in your own code and so might clash with Microsoft's usage. _WIN32 is 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),因為它以一個下划線和大寫字母開頭——您不能在自己的代碼中定義保留的名稱,因此不存在沖突。

#2


68  

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

尼爾·巴特沃思和藍色。燕尾服已經給出了正確的答案):

  • WIN32 is defined by the SDK or the build environment, so it does not use the implementation reserved namespace
  • WIN32是由SDK或構建環境定義的,因此它不使用實現保留的名稱空間
  • _WIN32 is defined by the compiler so it uses the underscore to place it in the implementation-reserved namespace
  • _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 by the compiler, they are used to control what the CRT headers do:

您將發現一組類似的雙重定義,具有幾乎相同的名稱和類似的用途,如_UNICODE/UNICODE、_DEBUG/DEBUG,或者可能是_DLL/DLL(我認為只有UNICODE的名稱在它們的不同版本中有很大用處)。雖然在這些情況下(比如_UNICODE),不是由編譯器定義的下划線版本,而是用來控制CRT頭的作用:

  • _UNICODE tells the CRT headers that CRT names which can be either Unicode or ANSI (such as _tcslen() should map to the wide character variant (wcslen())
  • _UNICODE告訴CRT頭文件,可以是Unicode或ANSI(比如_tcslen())的CRT名稱應該映射到寬字符變體(wcslen())
  • UNICODE does something similar for the SDK (maps Win32 APIs to their "W" variants)
  • 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 use the 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 _WIN32 when 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.

注意,在為Mac構建時,SDK將定義_WIN32,因為編譯器不會,有點超出它的界限。我不確定哪個項目使用Win32 API,哪個編譯器針對的是Mac——也許是某個版本的Office for the Max什么的。

#3


18  

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.

我更喜歡使用(read) _WIN32,對我來說更安全。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/19/724d6a7120a6d77065becde3d137b399.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com