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?
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.
Use toolchain
- 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.
- 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).
- 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.