问题描述
基本上,当我在Linux上进行开发时,我正在尝试将最简单的代码编译到Windows.
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
有没有办法在Windows上运行的Linux上编译代码?
Is there a way to compile code on Linux that will run on Windows?
推荐答案
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"
这篇关于从Linux到Windows交叉编译Rust应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!