如何在单个构建中使 CMake 面向多个平台 [英] How to make CMake targeting multiple platforms in a single build

查看:36
本文介绍了如何在单个构建中使 CMake 面向多个平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以某种方式配置我的 CMake 项目,以便单个构建执行针对多个平台(在我的情况下,我想为 Linux 和 Windows 构建,分别针对 x86_32 和 x86_64).我已经安装并运行了交叉编译器工具链,并为每个单独的目标构建工作.

I'd like to configure my CMake project in a way, so that a single build execution targets multiple platforms (in my case I'd like to build for Linux and Windows, x86_32 and x86_64 targets each). I have the cross compiler toolchains installed and working, and building for each individual target works.

因此,挑战在于以某种方式设置 CMake,即为每个子构建适当设置工具链和 CMAKE_SYSTEM_... 变量.我该怎么做?

So the challenge is setting up CMake in a way, that the toolchain and the CMAKE_SYSTEM_… variables are set appropriately for each sub-build. How can I do that?

推荐答案

这是不可能的,一般来说.

This is not possible, in general.

CMake 只允许一次配置单个目标平台.如果您想针对不同的平台,则需要从头开始重新运行 CMake.当工具链的其余部分保持不变(例如,您不能同时构建 x86 和 x64 二进制文件)时,这甚至适用于仅切换架构.

CMake only allows configuring for a single target platform at once. If you want to target a different platform, you need to re-run CMake from scratch. This is even true for switching just architectures when the rest of the toolchain remains the same (for instance, you cannot build x86 and x64 binaries at the same time).

自动执行此操作的正确方法是拥有一个封闭的构建脚本,该脚本为每个目标平台调用一次 CMake,并对不同的二进制目录执行多个源外构建.这个封闭脚本也可以用 CMake 本身编写(ExternalProject 模块对此非常有效),尽管像 Python 或 Bash 这样的通用脚本语言可能更适合.

The proper way to automate this would be to have an enclosing build script which invokes CMake once for each target platform and performs several out-of-source builds to distinct binary directories. This enclosing script could also be written in CMake itself (the ExternalProject module works quite well for this), although a general purpose scripting language like Python or Bash might be a better fit.

这篇关于如何在单个构建中使 CMake 面向多个平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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