Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

architecture - CMake Multiarchitecture Compilation

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" pathosourcedir

    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 pathosourcedir

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 pathosourcedir

to compile 32bit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...