How to detect if 64 bit MSVC with cmake?

2019-02-08 06:12发布

I have a project which uses cmake, one target is set to only build with MSVC:

 if (MSVC)
     add_library(test SHARED source.cpp) 
 endif()

Now the other issue is that this target is only designed for MSVC 32bit. So how can I detect that the generator is MSVC64 and skip this target?

2条回答
戒情不戒烟
2楼-- · 2019-02-08 07:01

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

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    # 64 bits
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    # 32 bits
endif()
查看更多
爷的心禁止访问
3楼-- · 2019-02-08 07:04

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

查看更多
登录 后发表回答