在 Arch Linux 下为 Windows 编译 Rust 程序

栏目: 编程语言 · Rust · 发布时间: 5年前

内容简介:假设已经安装了 Rust 的工具链。这些都是 aur 上的包,因为我不想自己再编译一遍,就偷懒安装了在

假设已经安装了 Rust 的 工具 链。

添加 x86_64-pc-windows-gnu target:

rustup target add x86_64-pc-windows-gnu

安装 mingw-w64 工具链:

yay -S mingw-w64-crt-bin mingw-w64-binutils-bin mingw-w64-winpthreads-bin mingw-w64-headers-bin mingw-w64-gcc-bin

这些都是 aur 上的包,因为我不想自己再编译一遍,就偷懒安装了 *-bin

设置链接器

~/.cargo/config 中加入以下内容:

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"

[target.i686-pc-windows-gnu]
linker = "i686-w64-mingw32-gcc"

编译

差不多可以尝试编译了,我试了一下之前自己写的那个 sha256 库:

git clone https://github.com/nanpuyue/sha256
cd sha256
cargo build --release --target x86_64-pc-windows-gnu --example sha256sum

结果得到了如下错误:

= note: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /home/nanpuyue/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/crt2.o:crtexe.c:(.rdata$.refptr.__onexitbegin[.refptr.__onexitbegin]+0x0): undefined reference to `__onexitbegin'
          /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /home/nanpuyue/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/crt2.o:crtexe.c:(.rdata$.refptr.__onexitend[.refptr.__onexitend]+0x0): undefined reference to `__onexitend'
          collect2: 错误:ld 返回 1

查了一下,找到了这个: https://github.com/rust-lang/rust/issues/48272#issuecomment-429596397

简单的说就是 Rust 工具链中自带的 crt2.o 太老了,我们替换一下:

cp -vb /usr/x86_64-w64-mingw32/lib/crt2.o "$(rustc --print sysroot)"/lib/rustlib/x86_64-pc-windows-gnu/lib/

再重新编译,看起来问题不大:

在 Arch Linux 下为 Windows 编译 Rust 程序

它甚至能运行:

在 Arch Linux 下为 Windows 编译 Rust 程序


以上所述就是小编给大家介绍的《在 Arch Linux 下为 Windows 编译 Rust 程序》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

分布式算法导论

分布式算法导论

特尔 (Gerard Tel) / 电子工业出版社 / 2003-7 / 59.00

《分布式算法导论(第2版)(英文版)》由电子工业出版社出版。一起来看看 《分布式算法导论》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具