CMake 多架构编译 [英] CMake Multiarchitecture Compilation

查看:49
本文介绍了CMake 多架构编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何编写允许使用任何编译器和操作系统编译 x86 和 x64 架构的 CMake 设置.

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.

推荐答案

如果 CMake 有一个开箱即用的 32/64 位选项,那就太好了.它没有,因此您需要应用不同的编译器或生成器依赖方法之一.例如:

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(在 Linux 上)和其他一些编译器,例如太阳工作室.设置 CFLAGSCXXFLAGS 以包含 -m32(32 位构建)或 -m64(64 位构建)).

  • 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 生成器.使用 64 位生成器,例如

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

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

编译 64 位 (x64).在生成器名称中省略Win64",为 32 位构建

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

Mac OS X.使用 CMAKE_OSX_ARCHITECTURES CMake 变量.

Mac OS X. Use CMAKE_OSX_ARCHITECTURES CMake variable.

cmake -DCMAKE_OSX_ARCHITECTURES=i386/path/to/source/dir将编译 32 位构建

cmake -DCMAKE_OSX_ARCHITECTURES=x86_64/path/to/source/dir将编译 64 位.

cmake "-DCMAKE_OSX_ARCHITECTURES=x86_64;i386"/path/to/source/dir 将创建 96 位通用二进制文件 :)

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

以上内容略有改写.

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

2019 年 4 月更新

这是一个旧答案,有些东西已经改变,特别是对于 Windows 上的 VS 生成器.

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

使用 cmake 3.14 和 Visual Studio 2019 安装

with cmake 3.14, and Visual Studio 2019 installed

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

编译 64 位 (x64).您通常可以在 x64 机器上省略 -G 和 -A 参数(您在 2019 年使用的是 x64 操作系统,对吗?)

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

编译32位.

这篇关于CMake 多架构编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆