# How to build for Win32 by default when using Visual Studio 2019 generator?

**URL:** https://discourse.cmake.org/t/how-to-build-for-win32-by-default-when-using-visual-studio-2019-generator/5640
**Category:** Code
**Tags:** os:windows, gen:vs
**Created:** [May 10, 2022, 3:59pm UTC](https://discourse.cmake.org/t/how-to-build-for-win32-by-default-when-using-visual-studio-2019-generator/5640 "2022-05-10T15:59:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [May 10, 2022, 3:59pm UTC](https://discourse.cmake.org/t/how-to-build-for-win32-by-default-when-using-visual-studio-2019-generator/5640/1 "2022-05-10T15:59:21Z")

</div>

a.k.a. _ **"How to force architecture of the build in the project itself?"** _

I am currently porting to CMake a VS project that can be built only as 32-bit application. Yet for modern VS versions the default bitness is “same-as-host” - so when the project is configured and built on a regular x64 system with simple `cmake -B builddir && cmake --build builddir`, everything falls apart. The correct incantation for configuration should be `cmake -B builddir -A Win32`. But the less flags you have to remember for the build - the better.

As I am for a moment not limited by a CMake Version, I figured I could finally try out CMake Presets. This `CMakePreset.json` seems to do the job:

```json
{
  "version": 2,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 20,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "default",
      "generator": "Visual Studio 16 2019",
      "architecture": "Win32",
      "binaryDir": "${sourceDir}/build/default"
    }
  ],
  "buildPresets": [
    {
      "name": "default",
      "configurePreset": "default"
    }
  ]
}

```

, so the project now can be built like this:

```auto
cmake --preset default
cmake --build --preset default

```

This kinda pins the VS to 2019, so for 2022 and further I would have to also create other presets, but it is okay for now.

However, suppose I am stuck with some older version, say 3.18 - just before the presets were introduced. What would be my options then?

- There is [`CMAKE_GENERATOR_PLATFORM`](https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_PLATFORM.html) - “Generator-specific target platform specification _provided by user_”. If I use this in my project:

```cmake
cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_GENERATOR_PLATFORM Win32)
project(Stuff)

```

, then the solution created actually contains `Win32` platform instead of `x64` - but building it causes error:

```auto
$ cmake -B build/2
...
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x86/cl.exe - skipped
...
$ cmake --build build/2
...
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(436,5): error MSB8013: This project doesn't contain the Configuration and Platform combination of Debug|x64. [S:\build\2\ZERO_CHECK.vcxproj]

```

- The doc says that "_The value of this variable should never be modified by project code. A toolchain file specified by the [`CMAKE_TOOLCHAIN_FILE`](https://cmake.org/cmake/help/latest/variable/CMAKE_TOOLCHAIN_FILE.html#variable:CMAKE_TOOLCHAIN_FILE) variable may initialize `CMAKE_GENERATOR_PLATFORM`_". But even if I do this:

```cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake-default-toolchain.cmake)
project(Stuff)

# cmake-default-toolchain.cmake
set(CMAKE_GENERATOR_PLATFORM Win32)

```

, the result is still the same.

- Finally, there is the mention that “_On [Visual Studio Generators](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#visual-studio-generators) the selected platform name is provided in the [`CMAKE_VS_PLATFORM_NAME`](https://cmake.org/cmake/help/latest/variable/CMAKE_VS_PLATFORM_NAME.html#variable:CMAKE_VS_PLATFORM_NAME) variable._”. If I replace setting `CMAKE_GENERATOR_PLATFORM` with `CMAKE_VS_PLATFORM_NAME`, the variable is seemingly ignored. `CMAKE_VS_PLATFORM_NAME_DEFAULT` also does not help, leading me to believe they are read-only variables used simply to indicate from inside CMake itself, which platform was used.

- _However_, if I pass `-DCMAKE_GENERATOR_PLATFORM=Win32` during configuration from the commandline, the build works fine! But that is literally same as `-A Win32`, even more verbose.

Any suggestions?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 10, 2022, 6:01pm UTC](https://discourse.cmake.org/t/how-to-build-for-win32-by-default-when-using-visual-studio-2019-generator/5640/2 "2022-05-10T18:01:21Z")

</div>

Short answer: you don’t. The user gets to choose the toolchain and target. All you can (and should) do is check it and message:

- error if you know it isn’t supported (e.g., some Unix platform)
- warn if it is some adjacent platform (e.g., Windows Phone)

You can certainly indicate that the user should use `--preset vs2019` or `--preset vs2022` for easier usage.

---

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [May 11, 2022, 8:38am UTC](https://discourse.cmake.org/t/how-to-build-for-win32-by-default-when-using-visual-studio-2019-generator/5640/3 "2022-05-11T08:38:07Z")

</div>

Alas! But thanks for clarification. SO replies juggling around toolchains and CMAKE\_GENERATOR\_PLATFORM made me think it was still possible and I was just looking in the wrong directions.
