Generating 64-bit projects for Visual Studio with CMake

Generator

Hm, interesting… I thought for some reason that by invoking CMake from the 64-bit Visual Studio command prompt, the project would automatically be generated as a 64-bit target — turns out that this is not the case: Checking the Solution File, the configuration is still set to Win32 (instead of x64) and in the project properties under Linker → Advanced, the Target Machine is still set to /MACHINE:X86 (instead of X64).

So one has to specifically tell CMake to create a 64-bit project:

C:\projectx\build> cmake ..\make -G "Visual Studio 10 Win64"

Library paths

Also, make sure to include the right paths for the 64-bit libraries if you build a 64-bit target; in one case, I only added the top-level directory of the Windows SDK instead of the required sub-folder, which resulted in a lot of linker errors (LNK2019: unresolved external symbol… etc.).

C:/Program Files/Microsoft SDKs/Windows/v7.1/Lib/x64

Check in CMakeLists.txt

As far as I could find out, CMake doesn’t offer an easy way to detect the architecture of the target platform (but then, how could it?), so the CMakeLists.txt for my Windows-/Visual-Studio-64-bit-only project has currently the following condition at the beginning:
if (${CMAKE_GENERATOR} MATCHES "Visual Studio" AND ${CMAKE_GENERATOR} MATCHES "64")
Maybe not the most elegant solution, but it works for the moment.

(2017-03-30: Small update to this post, thanks to a helpful tip from Robin Degen [when this blog was running with WordPress and had an internal commenting system].)

And indeed, in the years since I wrote this (and learned more about CMake), I discovered the popular idiom to check whether the build is done for(!) a 64-bit target and by now, practically all my CMakeLists contain boilerplate code like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
if (${CMAKE_SIZEOF_VOID_P} EQUAL "8")
    set (PROJECTNAME_ARCHITECTURE "x64")
else ()
    set (PROJECTNAME_ARCHITECTURE "x86")
endif ()

if (MSVC)                                                        # - Microsoft Visual C++
    if (CMAKE_CL_64 OR ${PROJECTNAME_ARCHITECTURE} EQUAL "x64")  # -- 64-bit builds.
        # [...]
    else ()                                                      # -- 32-bit builds.
        # [...]
    endif ()
        # [...]
else ()	
    message(FATAL_ERROR "\nError: Only Microsoft Visual Studio is currently supported.\n")
endif ()