Avatar ·

如何在Linux上为Windows编译Rust应用?从零开始的交叉编译

大家好!我在Linux上用Rust编写一个小型CLI应用,但需要它也能在Windows上运行。问题在于我不想为了编译而切换到Windows——我想直接在Ubuntu下编译出exe文件。 我通过`rustup target add`添加了`x86_64-pc-windows-gnu`目标,但尝试用`cargo build --target x86_64-pc-windows-gnu`编译时遇到了大量链接错误,例如: ``` error: linking with `x86_64-w64-mingw32-gcc` failed: exit status: 1 | = note: /usr/bin/x86_64-w64-mingw32-ld: cannot find -ladvapi32 /usr/bin/x86_64-w64-mingw32-ld: cannot find -luserenv ``` 我了解到需要MinGW-w64,通过apt安装了它,但错误仍然存在。我也尝试过安装`mingw-w64`——没有帮助。我的代码很简单: ```rust use std::process::Command; fn main() { let output = Command::new("cmd") .args(&["/C", "echo Hello from Windows!"]) .output() .expect("failed to execute process"); println!("{}"