Hi all,
Is there any CMake variable for distinguish, on Windows, if we are building for 32 bits Architecture or 64 bits Architecture?
Thanks in advance for any help
Have a look at CMAKE_SIZEOF_VOID_P variable.
2 Likes
if (CMAKE_SIZE_OF_VOID_P EQUAL 4)
message(STATUS "32 bit")
endif()
if (CMAKE_SIZE_OF_VOID_P EQUAL 8)
message(STATUS "64 bit")
endif()
1 Like
Ok, but if I am on a 64 bits Host machine, but I build with -A 32 bits flag, the sizeof of the word is 4 bytes or 8 bytes? I think it will be the latter…
The variable CMAKE_SIZE_OF_VOID_P
describes your build environment, not the host on which you are running… So it will be 4 bytes.
1 Like
On Windows you basically have 4 different compilers (for x86/AMD64):
# C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC
# Host 64 -> Target 64
x64 Native Tools Command Prompt for VS 2022
# Host 64 -> Target 32
x64_x86 Cross Tools Command Prompt for VS 2022
# Host 32 -> Target 64
x86_x64 Cross Tools Command Prompt for VS 2022
# Host 32 -> Target 32
x86 Native Tools Command Prompt for VS 2022
I never recommend using the last 2 for larger applications since big projects tend to use more than 4 GB of RAM.
Anyway CMake lets you pick both your host tools and your target platform. They are different things.
From the CMake docs:
The CMAKE_GENERATOR_PLATFORM variable may be set, perhaps via the cmake(1) -A option, to specify a target platform name (architecture). For example:
- cmake -G "Visual Studio 17 2022" -A Win32
- cmake -G "Visual Studio 17 2022" -A x64
- cmake -G "Visual Studio 17 2022" -A ARM
- cmake -G "Visual Studio 17 2022" -A ARM64
If you want to choose different host tools here are the docs. However, VS2019 and newer use 64-bit tools by default.
1 Like