在Windows10 64位编译32位Rust程序

1、添加i686-pc-windows-msvc target

rustup target add i686-pc-windows-msvc

2、编译目标文件

cargo build --target=i686-pc-windows-msvc

如果编译成功,那么什么事也没了。 如果出现这样的错误:

error: linker `i686-pc-windows-msvc` not found
  |
  = note: 系统找不到指定的文件。 (os error 2)

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 was installed with the Visual C++ option

error: aborting due to previous error

应该是找不到对应targetlink.exe文件,需要在工程根目录下编写.cargo/config文件,加入以下内容:

[target.i686-pc-windows-msvc]
linker = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.24.28314\\bin\\Hostx64\\x86\\link.exe"

这里特别需要注意linker指向的link.exe文件,一定要指向x86的,跟你target相对应。

本博客采用 知识共享署名-禁止演绎 4.0 国际许可协议 进行许可

本文标题:在Windows10 64位编译32位Rust程序

本文地址:https://jizhong.plus/post/2020/01/cross-compilation.html