基本上,当我在 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 许可协议
其他答案虽然在技术上是正确的,但比他们需要的要困难得多。 There’s no need to use
rustc
(in fact it’s discouraged, just usecargo
), you only needrustup
,cargo
and your distribution’s mingw-w64。添加目标(您也可以为要交叉编译的任何目标更改它):
您可以通过以下方式轻松构建您的 crate:
无需搞乱
~/.cargo/config
或其他任何东西。编辑:只是想补充一点,虽然您可以使用上面的内容,但有时也令人头疼。我想补充一点,rust 工具团队还维护一个名为 cross 的项目: https ://github.com/rust-embedded/cross 这可能是您想要研究的另一个解决方案