rust语言程序在windows xp上运行的编译方法

rust语言不支持在windows xp上编译,目前(2018)只支持linux,unix,win7及以上系统上编译。

早期的时候rust不仅不能够在xp上编译,还不能够在xp上运行。经过不少人争取,尤其是Mozilla公司firefox的需要,rust语言对xp提供了一定支持,部分程序能够在xp上运行。(但是据说因为一些winxp api的问题,有一些代码编译后运行有问题,目前我还没有遇到)

那么应该怎么样编译才能够让rust程序在xp上运行呢?有两种方法:

  1. 使用msvc工具链,设置链接目标参数
  2. 使用gnu工具链编译

msvc工具链编译

windows xp对应的构架为i686,所以要安装对应的target。在cmd中运行如下命令安装:

rustup target add i686-pc-windows-msvc

方法一

使用rustc增加link-args及target参数,使编译的目标程序可以运行在xp上。

rustc main.rs -Clink-args=/subsystem:console,5.01 --target i686-pc-windows-msvc

当然console也可以用windows代替。

方法二

rust语言使用cargo可以直接编译整个项目,使用rustc不如使用cargo方便。如果想使用cargo,可以使用如下方式:

set -Clink-args=/subsystem:console,5.01
cd project
cargo project --target i686-pc-windows-msvc

使用x86_64-pc-windows-msvc工具链也能编译target为target i686-pc-windows-msvc的程序,不需要再下载i686-pc-windows-msvc工具链

当然使用i686-pc-windows-msvc工具链编译target为target i686-pc-windows-msvc的程序也是可以的

rust程序不依赖msvc库运行的方法

windows上使用msvc工具链编译的rust程序,需要msvc库才能运行,为什么呢?这是因为为了在windows上运行,rust链接了部分msvc的函数。在编译的时候设置静态链接,就可以消除运行时依赖。

方法如下:

rustc -Ctarget-feature=+crt-static  main.rs

或者

set -Ctarget-feature=+crt-static
cd project
cargo project --target i686-pc-windows-msvc

编译xp目标rust程序推荐方法

把如下代码保存在bat脚本文件中,打开脚本后,直接使用cargo build或者cargo run就可以了

@echo off
cd %cd%
set RUSTFLAGS=-Ctarget-feature=+crt-static -Clink-args=/subsystem:console,5.01
doskey cargo1=cargo $* --target i686-pc-windows-msvc
cmd /k "title rust4xp & echo "Ready for copile rust progame runing on winxp!"

当然,也可以通过添加build.rs配置,比较麻烦,有兴趣的同学可以研究一下。

gnu 工具链编译

在windows上官方比较推荐的是使用msvc编译,当然也可以使用gnu编译工具编译。msvc工具链和gnu工具链编译后的rust程序都能正常运行,但是两者编译的目标程序ABI不同,目前对我个人来说影响不大,有兴趣的可以继续了解一下。

使用gnu工具链编译xp上运行的rust程序很简单。

  1. 安装工具链
    1. 安装32bit的mingw:在msys2上直接使用pacman -S mingw-w64-i686-gcc
    2. 安装i686 gnu工具链:rustup toolchain install stable-i686-pc-windows-gnu
    3. 添加i686 gnu target: rustup target add i686-pc-windows-gnu
  2. 设置工具链:rustup default stable-i686-pc-windows-gnu
  3. 编译:rustc main.rs或者cargo build

gnu工具链编译的rust程序不依赖gnu库就可以运行。

gnu工具链编译xp上运行的rust程序,必须安装32bit的mingw,必须使用i686-pc-windows-gnu工具链,必须使用i686-pc-windows-gnu target。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hustlei

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值