Rust: win10环境如何编译bevy?

bevy是一个rust语言的跨平台游戏引擎,但是在windows10环境编译时,遇到问题无数,幸尽数解决,记录于此,供遇到同类问题的朋友参考:

环境:windows 10 + bevy 0.5.0 + rustup nightly版本

 

一、设置crates国内镜像

crates.io官网国内访问实在太慢,可在当前用户的.cargo目录下,创建名为config的文件

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 这时换成自己偏好的源即可
replace-with = 'rustcc'

# 中国科技大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

# rustcc社区
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"

  

二、安装vs2019 build tools

bevy book上明确说明windows环境需安装VS2019 build tools (注意:必须是vs2019,最新的版本反而会编不过)

MS官网要找到vs2019,得费一番周章,点完一堆链接,还要登录后,才能找到旧版本的下载地址

https://my.visualstudio.com/Downloads?q=visual%20studio%202019&wt.mc_id=o~msft~vscom~older-downloads

安装完成后,需将64位的link.exe文件所在位置,加入到path环境变量中(重要!),我机器上安装完是下面这个目录 

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.20.27508\bin\Hostx64\x64\

 

三、linking with `link.exe` failed: exit code: xxxx 问题

如果遇到类似下面的错误:

linking with `link.exe` failed: exit code: 1189
...
...
= note: Non-UTF-8 output: LINK : fatal error LNK1189: \xb3\xac\xb9\xfd 65535 \xb6\xd4\xcf\xf3\xb5\xc4\xbf\xe2\xcf\xde\xd6\xc6\r\n

看到这种出错信息,通常颇感无助,但其实最后1行是有用的,只是由于编码问题,看不出其中的含义,有一个简单的办法,倘若机器上安装有python,直接用下面的2行代码稍做处理

content = b"fatal error LNK1189: \xb3\xac\xb9\xfd 65535 \xb6\xd4\xcf\xf3\xb5\xc4\xbf\xe2\xcf\xde\xd6\xc6\r\n"
print(content.decode("gbk"))

可以看到

fatal error LNK1189: 超过 65535 对象的库限制 

再根据这个信息,到网上一通搜索,终于找到某位高人留下的只言片语:

\.
│   Cargo.toml
│
├───.cargo
│       config.toml
│
└───src
        main.rs

在项目根目录下,创建1个.cargo的目录,然后里面放1个config.toml,内容为:

[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
rustflags = ["-Zshare-generics=off"]

先cargo clean,清空原来的target输出,再运行cargo run,可能又会遇到下面的错:

 error: the option `Z` is only accepted on the nightly compiler

意思是只有nightly版本,才能使用Z开头的参数。

rustup install nightly

先运行这行命令,安装nightly版本,然后将默认版本切换到nightly

rustup default nightly

注:如果希望切换回stable版本,只需运行rustup default stable即可。

可以输入如下命令确认下版本:

rustc --version

笔者机器上,会输出:

rustc 1.59.0-nightly (0b42deacc 2021-12-09)

这些弄完后,再运行

cargo clean
cargo run

应该就可以了,如果cargo clean时,遇到“target\xxx.dll 无法删除”云云,手动干掉target目录即可。

 

四、关于编译慢的问题

bevy book上提到,强烈建议windows用户安装cargo-binutils

cargo install -f cargo-binutils
rustup component add llvm-tools-preview

然后项目的Cargo.toml里,

[dependencies]
bevy = { version = "0.5.0", features = ["dynamic"] }

写上dynamic,这样会大大提高编译速度。

 

最后贴1个官方example的运行示例:

cargo run --example z_sort_debug

posted @ 2021-12-11 15:01  菩提树下的杨过  阅读(641)  评论(0编辑  收藏  举报