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

查看:381
本文介绍了如何在单个构建中使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?

推荐答案

通常这是不可能的。

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天全站免登陆