首页 > 解决方案 > 如何使用 Rust 和 Amethyst 运行可执行文件

问题描述

所以我遵循了 Amethyst Pong 教程,现在正在构建一个小的生命游戏程序。如果我运行它,它工作正常,cargo run但如果我这样做cargo build然后运行

$ .\target\debug\game_of_life.exe

我得到错误:

Error: Error { inner: Inner { source: None, backtrace: None, error: ConfigError(File(Os { code: 3, kind: NotFound, message: "The system cannot find the path specified." })) } }

如果还不清楚我在 Windows 10 上。我还创建了一个空白 rust 项目并尝试运行它的可执行文件,它运行良好:

$ cargo new temp
$ cd temp
$ cargo build
$ .\target\debug\temp.exe
Hello, world!

重现步骤(必须安装 cargo 和 vulkan):

$ cargo install amethyst_tools
$ amethyst new temp
$ cd temp
$ cargo build
$ .\target\debug\temp.exe
Error: Error { inner: Inner { source: None, backtrace: None, error: ConfigError(File(Os { code: 3, kind: NotFound, message: "The system cannot find the path specified." })) } }

请注意:

$ amethyst new temp
$ cd temp
$ cargo run

工作正常

版本:

$ amethyst --version
Amethyst CLI 0.10.0
$ cargo --version
cargo 1.43.0 (3532cf738 2020-03-17)

我应该提供任何想法或更多信息吗?

标签: rustamethyst

解决方案


您的主函数调用application_root_dir,它是 Amethyst 的一部分。application_root_dir的定义表明它正在使用CARGO_MANIFEST_DIR或您的可执行文件的位置作为您的根路径(稍后用于查找资产和配置)。当您调用cargo run时,它会设置CARGO_MANIFEST_DIR为当前构建的 crate 的 Cargo.toml 的目录,而如果您直接调用二进制文件,CARGO_MANIFEST_DIR则根本不会设置(因此它将尝试.\target\debug用作查找配置/资产的基本路径)。

您可以将二进制文件复制到您的位置Cargo.tomlCARGO_MANIFEST_DIR手动设置,然后您应该能够直接执行您的二进制文件。


推荐阅读