CMake Multiarchitecture Compilation

2019-01-12 22:14发布

问题:

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:

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



回答2:

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.



回答3:

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