Option to force either 32-bit or 64-bit build with cmake

Flogo picture Flogo · Jun 30, 2015 · Viewed 34.4k times · Source

I would like to offer a way that always builds my target as a 32-bit or always as 64-bit executable executable with cmake independent of the host system (Adding the "-m32" or "-m64" flag for gcc, not sure yet what to do for other compilers).

I can think of three ways to do this, which one should I use?

  1. an option (-DUSE32bit=true)
  2. a tool chain (-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)
  3. build types (-DCMAKE_BUILD_TYPE=release32)

In my case the forced 32-bit build will be the default and should be easy to use. A forced 64-bit build is the also useful for some cases and should not be too difficult. Using the bit width of the host system rarely makes sense for my case and I don't want to support it.

I found a related question here (The proper way of forcing a 32-bit compile using CMake) but the answers mostly discuss how it can be done at all, not how best to make it configurable.

Answer

tresf picture tresf · Dec 26, 2017

For Visual Studio and per https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_PLATFORM.html

For Visual Studio Generators with VS 2005 and above this specifies the target architecture.

cmake . -DCMAKE_GENERATOR_PLATFORM=x64