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

新手上路,请多包涵

基本上,当我在 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

可悲的是,它们都不起作用。它给了我一个关于 std crate 丢失的错误

$ 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 上运行的代码?

原文由 Fedcomp 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

其他答案虽然在技术上是正确的,但比他们需要的要困难得多。 There’s no need to use rustc (in fact it’s discouraged, just use cargo ), you only need rustup , cargo and your distribution’s mingw-w64。

添加目标(您也可以为要交叉编译的任何目标更改它):

 rustup target add x86_64-pc-windows-gnu

您可以通过以下方式轻松构建您的 crate:

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

无需搞乱 ~/.cargo/config 或其他任何东西。

编辑:只是想补充一点,虽然您可以使用上面的内容,但有时也令人头疼。我想补充一点,rust 工具团队还维护一个名为 cross 的项目: https ://github.com/rust-embedded/cross 这可能是您想要研究的另一个解决方案

原文由 zee 发布,翻译遵循 CC BY-SA 4.0 许可协议

Rust 发行版只为主机系统提供编译库。但是,根据 Arch Linux 在 Rust 上的 wiki 页面,您可以从 下载目录 中的 Windows 包(注意有 i686 和 x86-64 包)复制编译的库到您系统上的适当位置(在 /usr/lib/rustlib/usr/local/lib/rustlib ,取决于安装 Rust 的位置),安装 mingw-w64-gcc 和 Wine,您应该能够交叉编译。

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

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

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

然后你可以像这样使用 Cargo:

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

原文由 Francis Gagné 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题