Why does CMake default to Program Files x86 when compiling 64 bit program?

Viewed 433

I am trying to build and install a basic program with CMake 3.17.2 for 64 bit windows with Visual Studio 16 2019.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13.5)

project(Test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "/EHsc")

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  message("IS DEFAULT: ${CMAKE_INSTALL_PREFIX}")
else()
  message("NOT DEFAULT: ${CMAKE_INSTALL_PREFIX}")
endif()

message("PLATFORM = ${CMAKE_VS_PLATFORM_NAME_DEFAULT}")

add_executable(main main.cpp)

install(TARGETS main DESTINATION bin)

However, when running the following commands:

$ mkdir build
$ cd build
$ cmake ..

I get the following output:

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.27.29111.0
-- The CXX compiler identification is MSVC 19.27.29111.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
IS DEFAULT: C:/Program Files (x86)/Test
PLATFORM = x64
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/.../cmake-test/build

Why is the CMAKE_INSTALL_PREFIX defaulting to Program Files x86?

The CMake docs for CMAKE_INSTALL_PREFIX suggest it should be otherwise:

This variable defaults to /usr/local on UNIX and c:/Program Files/${PROJECT_NAME} on Windows.

Building confirms that the exe is really 64 bits:

$ cmake --build .
$ file Debug/main.exe
Debug/main.exe: PE32+ executable (console) x86-64, for MS Windows

Confirming that CMake itself is 64 bits, and explicitly running with that, gives the same output:

$ file /c/Program\ Files/CMake/bin/cmake.exe
/c/Program Files/CMake/bin/cmake.exe: PE32+ executable (console) x86-64, for MS Windows

# in CMD to remove git bash from the equation...
$ echo %PROGRAMFILES%
C:\Program Files
$ "C:\Program Files\CMake\bin\cmake.exe" ..
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
...
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
...
IS DEFAULT: C:/Program Files (x86)/Test
PLATFORM = x64
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/.../cmake-test/build
1 Answers

I believe this was happening because, while CMake was assuming VS 2019 as the generator, it was not assuming x64 as the architecture.

The architecture can be set with the -A flag:

cmake -G "Visual Studio 17 2022" -A x64

Explicitly setting the generator may or may not be necessary. Regardless, that seems to do the trick for the install.

Related