简体   繁体   English

如何在 MacOS 上编译 Rust 以在 AWS EC2 实例上运行? “无法执行二进制文件:Exec 格式错误”

[英]How do I compile Rust on MacOS, to be run on an AWS EC2 instance? "cannot execute binary file: Exec format error"

Problem问题

I've written some fairly simple Rust code on a Macbook, to be deployed on an AWS EC2 instance but when I SCP the binary across and run it, I am met with " bash-program-cannot-execute-binary-file-exec-format-error "我在 Macbook 上编写了一些相当简单的 Rust 代码,用于部署在 AWS EC2 实例上,但是当我通过 SCP 二进制文件并运行它时,我遇到了“ bash-program-cannot-execute-binary-file-exec -格式错误

Mac Details Mac 详细信息

  • ProductName: macOS产品名称:macOS
  • ProductVersion: 12.4产品版本:12.4
  • BuildVersion: 21F79构建版本:21F79

AWS Details AWS 详情

  • Instance type: t3.micro实例类型:t3.micro
  • uname -r : 5.10.0-10-cloud-amd64 uname -r : 5.10.0-10-cloud-amd64

Attempted Solution尝试的解决方案

I understand that it's a mismatch between the architecture of my machine vs the EC2 machine and since the EC2 is Debian, I'm assumed that the target type I want is "aarch64-unknown-linux-gnu", but running cargo build --target=aarch64-unknown-linux-gnu results in "error: linking with cc failed: exit status: 1 " with a very lengthy note.我知道我的机器与 EC2 机器的架构不匹配,因为 EC2 是 Debian,我假设我想要的目标类型是“aarch64-unknown-linux-gnu”,但是运行cargo build --target=aarch64-unknown-linux-gnu导致“error: linking with cc failed: exit status: 1”,并附有很长的注释。

The easiest way I've found to do a similar thing is use cross .我发现做类似事情的最简单方法是使用cross This is a cargo extension that will pull in all the dependencies needed to cross compile onto a different architecture.这是一个货物扩展,它将引入交叉编译到不同架构所需的所有依赖项。 The command format is deliberately identical to cargo:命令格式特意与 cargo 相同:

eg例如

cross build --release --target aarch64-unknown-linux-gnu

You can find instructions to set it up on their wiki page: https://github.com/cross-rs/cross#installation您可以在他们的维基页面上找到设置说明: https://github.com/cross-rs/cross#installation

As for the actual error, I believe the problem with just setting the target in cargo is that rust will still depend on the installed libc which will be for the native architecture.至于实际错误,我认为仅在 cargo 中设置目标的问题是 rust 仍将取决于已安装的 libc,它将用于本机架构。 cross gets round this by using docker images. cross使用 docker 张图像来解决这个问题。

it looks like the target architecture is not arm-based.. AMD should require an x86_64 target.看起来目标架构不是基于 arm 的。AMD 应该需要一个 x86_64 目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM