Flogo
Flogo

Reputation: 1868

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

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.

Upvotes: 31

Views: 74369

Answers (4)

lousticator
lousticator

Reputation: 11

thanks for all your inputs, but on my side i've finally chosen the -m32 hack with cmake

# cmake windows 32 bits mode bug: 32 bits link mode must be explicit (otherwise cmake will always guess a 64 bits target)
#  1- run "vcbarsall.bat x86" to setup Visual Studio build profile
#  2- "set cflags=-m32" and "set cxxflags=-m32"
#  3- let the cmake plugin "automatically" guess the target platform

Upvotes: 1

TarmoPikaro
TarmoPikaro

Reputation: 5223

Personally I wanted to switch to 32/64-bit projects within same solution for Visual studio / cmake configuration. I've figured out that this can be done using

set_target_properties(${project} PROPERTIES LINK_FLAGS ${PROJ_LINK_FLAGS})

where PROJ_LINK_FLAGS can be either /MACHINE:X86 or /MACHINE:X64 depending on compilation.

Upvotes: 1

tresf
tresf

Reputation: 7922

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

Upvotes: 34

user2288008
user2288008

Reputation:

TL;DR

Use toolchain

In depth

  1. an option (-DUSE32bit=true)

This is not scalable I guess. So what if you want to build N projects? You have to add N options.

  1. build types (-DCMAKE_BUILD_TYPE=release32)

This may work well. But in my opinion you're mixing unrelated stuff. Also I'm sure you have to adapt find_package behaviour by setting some *_ROOT CMake variables. It's not possible to do it with CMAKE_BUILD_TYPE (at least, again, in a scalable fashion).

  1. a tool chain (-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)

The best variant. If you want to build two projects - just use same toolchain:

cmake -Hproj-1 -B_builds/proj-1 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
cmake -Hproj-2 -B_builds/proj-2 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain

If you want to build your 3rd party ExternalProject_Add with 64 bit architecture - just pass toolchain to CMAKE_ARGS:

ExternalProject_Add(
    ...
    CMAKE_ARGS ... -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
    ...
)

Want to adapt find_package - just add any CMake variables to toolchain file.

Upvotes: 10

Related Questions