CMake Multiarchitecture Compilation

ArchitectureCompilationCmake

Architecture Problem Overview


I want to know how I could write a CMake setup which allows compilation for both x86 and x64 architectures using any compiler and OS.

Architecture Solutions


Solution 1 - Architecture

It would be great if CMake had an 32/64bit option out of the box. It does not, so you will need to apply one of different compiler or generator dependend methods. E.g.:

  • GCC (on Linux) and some other compilers, e.g. Sun Studio. Set CFLAGS and CXXFLAGS to include -m32 (32-bit build) or -m64 (64-bit build).

  • Windows, Visual Studio generator. Use 64 bit generator, e.g.

cmake -G "Visual Studio 10 Win64" path\to\source\dir

to compile 64-bit (x64). Omit "Win64" in generator name, to build for 32 bit

  • Mac OS X. Use CMAKE_OSX_ARCHITECTURES CMake variable.

    cmake -DCMAKE_OSX_ARCHITECTURES=i386 /path/to/source/dir will compile 32 bit build

    cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 /path/to/source/dir will compile 64 bit.

    cmake "-DCMAKE_OSX_ARCHITECTURES=x86_64;i386" /path/to/source/dir will create 96-bit universal binaries :)

The above is slightly reworded.

http://dev.mysql.com/doc/internals/en/compiling-for-different-hardware-achitectures.html

Update April 2019

This is an old answer, some things have changed, especially for VS generator on Windows.

with cmake 3.14, and Visual Studio 2019 installed

cmake -G "Visual Studio 16 2019" -A x64 path\to\source\dir

to compile 64-bit (x64). You can usually omit both -G and -A parameter on x64 machine (you're using x64 OS in 2019, right?)

cmake -G "Visual Studio 16 2019" -A Win32 path\to\source\dir

to compile 32bit.

Solution 2 - Architecture

If CMAKE_OSX_ARCHITECTURES=i386 gives you the error "unrecognized option -arch" start over and instead try:

cmake -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_SYSTEM_PROCESSOR=i386 -DCMAKE_SYSTEM_VERSION=10

Solution 3 - Architecture

There is a CMake tutorial available online to go over the basics, this is taken from the CMake book. The CMake philosophy is to use multiple build directories, with a single source tree. So you could use Visual Studio on Windows and create a build directory using the 32 bit compiler, and another using the 64 bit compiler.

CMake targets a large number of compilers and operating systems, but you don't mention what language you are using, what operating system/compiler/architectures you are thinking of and if you are using any toolkits/libraries.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOniLinkView Question on Stackoverflow
Solution 1 - ArchitectureVladislav VaintroubView Answer on Stackoverflow
Solution 2 - ArchitecturePatrick RoseView Answer on Stackoverflow
Solution 3 - ArchitectureMarcus D. HanwellView Answer on Stackoverflow