WIN32 Preprocessor definition in 64bit windows platform
Asked Answered
B

3

16

Should we change the preprocessor definition from WIN32 to WIN64 while migrating Visual 2012 C++ Projects to Target 64-Bit Platforms.

Now I have built the project with below settigns

  • MACHINE (Specify Target Platform) is set to /MACHINE:X64.

  • Target Environment is set to /env x64

  • in C/C++ project settings -> Code Generation, Struct Member Allignment to 8 BYtes

Please guide me what else project settings i should target to change.

Bromoform answered 29/6, 2013 at 12:47 Comment(1)
possible duplicate of Should I define both _WIN32 and _WIN64 in 64bit build?Opportunism
N
10

It's important to note that only the underscore versions of these preprocessor definitions are relevant to the distinction between 32-bit vs. 64-bit machines.

The underscore versions, _WIN32 and _WIN64 are built-ins that relate to the actual physical CPU of the computer that VC++ is running on. On a 32-bit machine, _WIN32 will always be defined (user does not need to define it), but _WIN64 will not be defined. On a 64-bit machine, _WIN64 will always defined and also _WIN32 may be defined but code can rely on _WIN64 to determine if machine is 64 bit.

For Visual Studio 2019 (perhaps other VS versions also):

The non-underscore WIN32 is not well documented and appears to have no bearing on 32 vs 64 machine type. Standard Visual C++ projects for Windows generally don't appear to use it (it may not be in use at all). Thank you to BTJ for making that point.

Another side note for Visual Studio: If you run Visual Studio on a 64-bit machine and select the Win32 vs x64 build configurations, you will notice that WIN32 is defined for the Win32 build configuration but it is not defined for an x64 build configuration. This does not affect the object/binary machine target e.g. 32 vs 64. It's purpose is unclear. It may be for convenience if one wishes to use it to #ifdef certain parts of the source code that are to be compiled differently for X86 vs X64, but again, it has no bearing on the architecture that the compiler targets. For the compiler, the target architecture is determined by the toolset that is selected based on the project target selected "Platform". The linker also has /MACHINE arg e.g. /MACHINE:X86.

Naominaor answered 4/8, 2018 at 5:53 Comment(2)
Do you have any evidence to support the following: For example, defining WIN32 (e.g. cl.exe ... /D WIN32 ...) causes VC++ to compile code for 32-bit architecture. My understanding is that the WIN32 symbol is ignored by VC++, and is therefore optional in all contexts.Alphitomancy
Thank you BTJ, very little clarity about why MS uses WIN32 in 32 bit projects, however you are correct. I tested in Visual Studio and 64 bit project linked fine regardless of whether WIN32 defined in compile step, and dumpbin /HEADERS showed .exe was x64. I also built 32 bit project without WIN32 and exe was 32 bit. Updated original post to reflect that aspect.Naominaor
P
5

Did you mean _WIN32 and _WIN64 macros? If you specified all parameters right (see P.S.) then you don't need change your code. In 64-bit solution must be defined _WIN32 and _WIN64 both. _WIN32 macro specifies that you can use Win32 API and _WIN64 macro specifies that compilation for 64-bit mode. Also you can use different macro for Itanium (_M_IA64) and x86-64 (_M_AMD64). See details in MSDN.

P.S. Did you choose platform parameters manually? You can specify it via VS:

  1. Build Menu -> Configuration Manager.
  2. Select New in Active Solution Platform.
  3. Type or select new platform -> x64 and click OK.
  4. Now in "Platform" row you can simple choose x64.
Purse answered 2/7, 2013 at 6:36 Comment(0)
T
0

In VS2015 at least, linker/All Options/Target Machine

this assumes that you're not using a 3rd-party tool (say widget support or hardware driver) that is configured to use x86 code or is built 32-bit while you're trying to build x64, or vice-versa

Turbofan answered 8/9, 2021 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.