基本上,当我在Linux上开发时,我正在尝试将最简单的代码编译到Windows中。
fn main() {
println!("Hello, and bye.")
}
我通过在互联网上搜索找到了这些命令:
rustc --target=i686-w64-mingw32-gcc main.rs
rustc --target=i686_pc_windows_gnu -C linker=i686-w64-mingw32-gcc main.rs
遗憾的是,它们都不起作用。它给我一个关于标准机箱丢失的错误
$ 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
有没有办法在Linux上编译可以在Windows上运行的代码?
发布于 2020-07-12 02:56:01
其他答案虽然在技术上是正确的,但却比需要的要困难得多。不需要使用rustc
(实际上这是不鼓励的,只需使用cargo
),您只需要rustup
、cargo
和您的发行版的mingw-w64即可。
添加目标(您也可以为您交叉编译的任何目标更改此设置):
rustup target add x86_64-pc-windows-gnu
rustup toolchain install stable-x86_64-pc-windows-gnu
您可以使用以下命令轻松构建您的板条箱:
cargo build --target x86_64-pc-windows-gnu
不需要摆弄~/.cargo/config
或其他任何东西。
编辑:只是想补充一下,虽然你可以使用上面的方法,但有时它也会让人头疼。我想补充的是,rust tools团队还维护了一个名为cross:https://github.com/rust-embedded/cross的项目,这可能是您想要研究的另一个解决方案
发布于 2015-07-19 00:01:59
Rust发行版仅为主机系统提供已编译的库。然而,根据Arch Linux's wiki page on Rust的说法,您可以将编译后的库从download directory中的Windows包(请注意,有i686和x86-64包)复制到您系统上的适当位置( /usr/lib/rustlib
或/usr/local/lib/rustlib
,取决于Rust的安装位置),安装mingw-w64- on和Wine,您应该能够进行交叉编译。
如果您正在使用Cargo,您可以通过将以下内容添加到~/.cargo/config
(其中$ARCH
是您使用的架构)来告诉Cargo在哪里查找ar
和链接器:
[target.$ARCH-pc-windows-gnu]
linker = "/usr/bin/$ARCH-w64-mingw32-gcc"
ar = "/usr/$ARCH-w64-mingw32/bin/ar"
注意:根据您的发行版,确切的路径可能会有所不同。检查您的发行版中的mingw-w64包(GCC和binutils)的文件列表。
然后你可以像这样使用货物:
$ # Build
$ cargo build --release --target "$ARCH-pc-windows-gnu"
$ # Run unit tests under wine
$ cargo test --target "$ARCH-pc-windows-gnu"
发布于 2021-03-19 02:41:41
对我有效的解决方案是。它类似于一个公认的答案,但我不需要添加工具链。
rustup target add x86_64-pc-windows-gnu
cargo build --target x86_64-pc-windows-gnu
有关更多详细信息,请参阅documentation。
https://stackoverflow.com/questions/31492799
复制相似问题