在 Arch Linux 下为 Windows 编译 Rust 程序

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

内容简介:假设已经安装了 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 程序》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

数据化运营速成手册

数据化运营速成手册

胡晨川 / 电子工业出版社 / 2017-4 / 55

《数据化运营速成手册》用于提升互联网公司员工的数据应用能力,即数据化运营能力。首先,从最常用的数据图表切入,帮助执行层正确地绘图,管理层正确地看图;接着,梳理运营中最基本的数据应用知识,涉及数据获取、数据清洗、数据认知、分析框架、指标体系、运营实验等内容。然后,介绍作者认为必要的统计学知识,包括假设检验、方差分析、回归分析和时间序列分解,并引入了管理科学中的规划求解方法。最后,介绍了数据分析工具的......一起来看看 《数据化运营速成手册》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器