如何从Mac交叉编译到Linux?

How to cross compile from Mac to Linux?

我用 Rust 写了一个小游戏,我用 cargo build --release 在 Mac 上编译了一个发布版本。

我试图与使用 Ubuntu 的朋友分享这个,但是当他尝试 运行 二进制文件时,他收到以下错误:

cannot execute binary file: Exec format error

我搜索过这个但没有找到答案。 Rust 不是声称有 "no runtime" 吗?它不应该能够 运行 二进制形式的任何地方吗?

嗯,因为 Rust 没有 运行时间(不像 Java 的 JVM)你不能只在上面编译代码一个 OS 并期望它在另一个 运行 上;你要找的是 cross-compilation. I haven't done it in Rust, but from what I can gather you can find relevant information on different cross-compilation Rust strategies on this GitHub repo.

Rust 没有运行时意味着它没有很多代码 运行 作为语言的一部分(例如垃圾收集器或字节码解释器)。它仍然需要使用操作系统原语(即系统调用),这些在 MacOS 和 Linux.

上是不同的

你要的是交叉编译器。如果您使用 rustup,那么 installing a cross compiler 应该很简单:

# Install the toolchain to build Linux x86_64 binaries
rustup target add x86_64-unknown-linux-gnu

那么建筑是:

cargo build --release --target=x86_64-unknown-linux-gnu

警告:我没有 OS X 机器来测试它;如果有效,请发表评论或编辑以解决此问题!