About VC pre-defined constants _win32,win32,_win64

Source: Internet
Author: User
Tags windows x64

VC2012 when writing Windows programs, it is sometimes necessary to determine the compilation environment. In the previous article, " judging whether the program is running under the Windows x64 system." said how to judge the system environment during operation, but how to judge at compile time?

The VC has 3 preprocessing constants, which are _win32,_win64,win32, MSDN says. How do these three constants work?   Looks simple, in fact is very confused. Under the Win32 configuration,WIN32 is declared in the project Properties-c/c++-preprocessor-preprocessor definition , and in x64 configuration, this constant is not in the project's predefined list. Does this mean that the x64 platform can be judged on the basis of WIN32? No. The 37th line under the Windows SDK minwindef.h is defined as follows:

#ifndef WIN32
#define WIN32
#endif

That is to say, as long as the Windows.h is included, the WIN32 constant is definitely defined, so it cannot be used to determine the platform environment。   However, if WIN32 is deleted in the preprocessing definition and does not contain Windows.h, then WIN32 is undefined. The following look at _win32 and _win64, these two are more special, without any explicit definition. Not in the Windows.h, inThere is also a "project property-c/c++-preprocessor-preprocessor definition ". According to MSDN, this is defined internally by the compiler (Ml.exe/ml64.exe). Specific description is  _win32:defined for Applications for Win32 and Win64. Always defined. _win64:Defined for Applications for Win64.Here's a program: (Run once in Win32 and x64 configurations, respectively)

#include <iostream>
Using NamespaceStd;
IntMain() {

#ifdef_win64
cout<< "_win64 is defined as" << _win64 << Endl #endif

#ifdef _win32
cout Span class= "pun" ><< "_WIN32 is defined as" << _win32 << Endl #endif

cin. ();
return 0 /span>

Under Win32 configuration, _WIN32 is defined and _win64 is undefined. In the x64 configuration, both are defined. That under VC, _win32 must have a definition。 As a result, win32/_win32 can be used to determine whether the Windows system (for cross-platform programs), while _win64 is used to determine if the compilation environment is x86 or x64. Finally, a table is attached:
Constants \ Definitions Pre-defined Options Windows.h VC Compiler
WIN32 Win32 √ (minwindef.h) x
_win32 X X
_win64 X X x64

About VC pre-defined constants _win32,win32,_win64 (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.