在 Arch Linux 下为 Windows 编译 Rust 程序

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

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

查看所有标签

猜你喜欢:

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

乌合之众

乌合之众

(法)勒庞 / 冯克利 / 中央编译出版社 / 2011-5-1 / 16.00元

古斯塔夫・勒庞 Gustave Le Bon(1841-1931) 法国著名社会心理学家。他自1894年始,写下一系列社会心理学著作,以本书最为著名;在社会心理学领域已有的著作中,最有影响的,也是这本并不很厚的《乌合之众》。古斯塔夫・勒庞在他在书中极为精致地描述了集体心态,对人们理解集体行为的作用以及对社会心理学的思考发挥了巨大影响。《乌合之众--大众心理研究》在西方已印至第29版,其观点新颖,语......一起来看看 《乌合之众》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具