bevy是一个基于rust语言的跨平台游戏引擎,但在windows 10环境下编译时遇到了许多问题,经过解决后,我将这些经验记录下来,供遇到类似问题的朋友参考:
环境: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' <h1>中国科技大学</h1><p>[source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index"</p><h1>清华大学</h1><p>[source.tuna] registry = "<a href="https://www.php.cn/link/33d208e8d6fc287973c46e5cb5346f06">https://www.php.cn/link/33d208e8d6fc287973c46e5cb5346f06</a>"</p><h1>上海交通大学</h1><p>[source.sjtu] registry = "<a href="https://www.php.cn/link/681e639bc756a64940c20e80ca055ae5">https://www.php.cn/link/681e639bc756a64940c20e80ca055ae5</a>"</p><h1>rustcc社区</h1><p>[source.rustcc] registry = "git://crates.rustcc.cn/crates.io-index"
二、安装VS2019 Build Tools
bevy书籍明确指出,Windows环境需要安装VS2019 Build Tools(注意:必须是VS2019,新版本可能无法编译通过)。在微软官网找到VS2019下载链接需要一些步骤,登录后才能找到旧版本的下载地址:
https://www.php.cn/link/eb27b5382e9d6e1ae84947113126b570
安装完成后,需要将64位的link.exe文件所在路径添加到PATH环境变量中(非常重要!),我的机器上安装后的路径是:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.20.27508\bin\Hostx64\x64\
三、解决link.exe链接错误
如果遇到类似下面的错误:
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
虽然错误信息看起来无助,但最后一行其实是有用的,只是由于编码问题看不出来。如果机器上安装了Python,可以使用以下两行代码进行处理:
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目录,并在其中添加一个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开头的参数。安装nightly版本并将其设置为默认版本:
rustup install 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书籍中提到,强烈建议Windows用户安装cargo-binutils:
cargo install -f cargo-binutils rustup component add llvm-tools-preview
然后在项目的Cargo.toml文件中,添加以下依赖:
[dependencies] bevy = { version = "0.5.0", features = ["dynamic"] }
这样可以大大提高编译速度。
最后,提供一个官方示例的运行命令:
cargo run --example z_sort_debug
以上就是Rust: win10环境如何编译bevy?的详细内容,更多请关注php中文网其它相关文章!
Windows激活工具是正版认证的激活工具,永久激活,一键解决windows许可证即将过期。可激活win7系统、win8.1系统、win10系统、win11系统。下载后先看完视频激活教程,再进行操作,100%激活成功。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号