且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Linux到Windows交叉编译Rust应用程序

更新时间:2023-10-22 23:30:16

Rust发行版仅为主机系统提供编译的库.但是,根据 Rust上Arch Linux的Wiki页面,您可以从下载目录(请注意,其中包含i686和x86-64软件包) /usr/lib/rustlib/usr/local/lib/rustlib,取决于Rust的安装位置),安装mingw-w64-gcc和Wine,您应该可以交叉编译.

The Rust distribution only provides compiled libraries for the host system. However, according to Arch Linux's wiki page on Rust, you could copy the compiled libraries from the Windows packages in the download directory (note that there are i686 and x86-64 packages) in the appropriate place on your system (in /usr/lib/rustlib or /usr/local/lib/rustlib, depending on where Rust is installed), install mingw-w64-gcc and Wine and you should be able to cross-compile.

如果您使用的是Cargo,则可以通过将其添加到~/.cargo/config(其中$ARCH是您使用的体系结构)中来告诉Cargo在哪里寻找ar和链接器:

If you're using Cargo, you can tell Cargo where to look for ar and the linker by adding this to ~/.cargo/config (where $ARCH is the architecture you use):

[target.$ARCH-pc-windows-gnu]
linker = "/usr/bin/$ARCH-w64-mingw32-gcc"
ar = "/usr/$ARCH-w64-mingw32/bin/ar"

注意:确切的路径可能会因您的分布而异.检查分发中mingw-w64软件包(GCC和binutils)的文件列表.

Note: the exact paths can vary based on your distribution. Check the list of files for the mingw-w64 package(s) (GCC and binutils) in your distribution.

然后您可以像这样使用货运:

Then you can use Cargo like this:

$ # Build
$ cargo build --release --target "$ARCH-pc-windows-gnu"
$ # Run unit tests under wine
$ cargo test --target "$ARCH-pc-windows-gnu"