简体   繁体   English

使用M1 mac将Rust程序编译成exe?

[英]Compile a Rust program to an exe using an M1 mac?

I have written a program in Rust on my M1 Mac, and compiled it to a Unix executable just fine.我在我的 M1 Mac 上写了一个 Rust 的程序,然后把它编译成 Unix 的可执行文件就好了。 Now I want to compile it to a Windows executable as well.现在我也想将它编译为 Windows 可执行文件。 I first tried我第一次尝试

$ cargo target add x86_64-pc-windows-gnu
$ cargo build --release --target=x86_64-pc-windows-gnu

I got the error message:我收到错误消息:

error: linker x86_64-w64-mingw32-gcc not found错误:找不到 linker x86_64-w64-mingw32-gcc

so I tried所以我试过了

$ brew install mingw-w64

according to this , and got the error:根据这个,得到了错误:

Error: mingw-w64: no bottle available!错误:mingw-w64:没有可用的瓶子!

I looked on formulae.brew.sh and it seems that mingw-w64 isn't supported for M1, only Intel.我查看了formulae.brew.sh ,似乎 M1 不支持mingw-w64 ,只有英特尔支持。

How can I compile an exe from rust using my M1 Mac?如何使用我的 M1 Mac 从 rust 编译一个 exe?

Solution 1:解决方案1:

You can install/add the targets using these commands:您可以使用以下命令安装/添加目标:

rustup target add x86_64-pc-windows-gnu
rustup toolchain install stable-x86_64-pc-windows-gnu

then you can target windows from your mac:然后你可以从你的mac定位 windows :

cargo build --release --target=x86_64-pc-windows-gnu

Solution 2: Simply use docker!解决方案2:只需使用docker!

FROM mcr.microsoft.com/windows/servercore:ltsc2019
// install rust
COPY project c:/project
RUN cd c:/project && cargo build --release

Solution 3: there is also Cross.解决方案3:还有Cross。 you can use this for cross-compilation: https://github.com/rust-embedded/cross您可以将其用于交叉编译: https://github.com/rust-embedded/cross

I'm going to jump in with a couple newer solutions that worked really well for me:我将加入一些对我来说非常有效的新解决方案:

cargo-zigbuild

Targets ( *-pc-windows-gnu afaik):目标( *-pc-windows-gnu afaik):

  • i686-pc-windows-gnu
  • x86_64-pc-windows-gnu

cargo-zigbuild also supports cross-compiling to other platforms out of the box as well, with no containerization needed! cargo-zigbuild还支持开箱即用地交叉编译到其他平台,无需容器化!

Quick example:快速示例:

brew install zig
cargo install cargo-zigbuild
cargo zigbuild --target x86_64-pc-windows-gnu -r

cargo-xwin

Targets ( *-msvc afaik):目标( *-msvc afaik):

  • aarch64-pc-windows-msvc
  • i586-pc-windows-msvc
  • i686-pc-windows-msvc
  • x86_64-pc-windows-msvc

Quick example:快速示例:

cargo install cargo-xwin
cargo xwin build --target x86_64-pc-windows-msvc -r

Which one do I pick?我选哪一个?

See: Difference between the gnu and msvc toolchains?请参阅: gnu 和 msvc 工具链之间的区别?

TL;DR: Use msvc / cargo-xwin for "more native" windows binaries (that are smaller in size) & use gnu / cargo-zigbuild to help porting over a linux-specific application. TL;DR:将msvc / cargo-xwin用于“更原生”的 windows 二进制文件(尺寸较小)并使用gnu / cargo-zigbuild来帮助移植特定于 linux 的应用程序。

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

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