简体   繁体   中英

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 "

Mac Details

  • ProductName: macOS
  • ProductVersion: 12.4
  • BuildVersion: 21F79

AWS Details

  • Instance type: t3.micro
  • 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.

The easiest way I've found to do a similar thing is use 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:

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

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. cross gets round this by using docker images.

it looks like the target architecture is not arm-based.. AMD should require an x86_64 target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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