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

Basically I'm trying to compile the simplest code to Windows while I am developing on Linux.

fn main() {

println!("Hello, and bye.")

}

I found these commands by searching the internet:

rustc --target=i686-w64-mingw32-gcc main.rs

rustc --target=i686_pc_windows_gnu -C linker=i686-w64-mingw32-gcc main.rs

Sadly, none of them work. It gives me an error about the std crate missing

$ rustc --target=i686_pc_windows_gnu -C linker=i686-w64-mingw32-gcc main.rs

main.rs:1:1: 1:1 error: can't find crate for `std`

main.rs:1 fn main() {

^

error: aborting due to previous error

Is there a way to compile code on Linux that will run on Windows?

解决方案

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.

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"

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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值