52

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.

1

3 Answers 3

98

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.

6
  • This is wonderful! But for Visual Studio, shouldn't it be -G since it's a generator? Also for Visual Studio, would it be possible to set some sort of flag in the CMakeLists.txt file to tell it automatically which to use (i.e. request that you select i386 or x86-64, and set it based on your response)?
    – OniLink
    Commented Mar 19, 2011 at 4:45
  • It should be -G, and the generator must be passed in on the command line. You can also use cmake-gui, a Qt based graphical interface, and pick the appropriate generator from the drop down list. Commented Mar 19, 2011 at 14:10
  • 1
    @OniLink, @Marcus. Yep, it should be -G . Thanks a lot for noticing! I fixed it now(and also fixed the same passage in the MySQL Wiki). To my knowlegde, changing bitness for Visual Studio inside CMakeLists.txt is not possible, bitness here is property of the generator, and generator cannot be set/changed dynamically. Commented Mar 19, 2011 at 21:02
  • 3
    With the MSVC toolchain you can also use set(CMAKE_EXE_LINKER_FLAGS "/machine:x86") (sub in your architecture) in your cmake files. Commented Mar 20, 2012 at 19:56
  • 2
    On Windows/MSVC, 64 and 32 bit are actually different compilers, unlike e.g gcc where architectue is a flag you'll pass to a compiler. Guess that's the reason. Commented Aug 27, 2015 at 19:55
2

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
-1

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.

1
  • 12
    The answer has nothing to do with the question.
    – Yola
    Commented Feb 5, 2018 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.