How to detect if 64 bit MSVC with cmake?

The usual way to check if you're generating for a 64 bits architecture is to test CMAKE_SIZEOF_VOID_P:

project( ... )
...

# won't work before project()!    
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    # 64 bits
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    # 32 bits
endif()

This variable is only set after the project command!


There are several ways - also used by CMake itself - that will check for "not 64Bit":

if(NOT "${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)")
    ...
endif()

if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
    ...
endif()

if(NOT CMAKE_CL_64)
   ...
endif()

References

  • CMAKE_GENERATOR
  • CMAKE_SIZEOF_VOID_P
  • CMAKE_CL_64