【问题标题】:How to include sdl2如何包含 sdl2
【发布时间】:2018-04-29 02:46:18
【问题描述】:

我想在 Rust 中使用 sdl2 包。

我的项目文件夹:

├── Cargo.toml
├── src
    ├── keyboard.rs
    └── main.rs

到目前为止,我做了以下工作: 在我的 Cargo 中添加了 sdl2 行,其中处理了外部依赖项: Cargo.toml:

// ...
[dependencies]
    sdl2 = "0.31"

要在我的keyboard.rs 中使用sdl2,我添加了以下几行:

extern crate sdl2;
use sdl2::keyboard::Keycode;

但是当我尝试编译 (cargo build) 时,我收到以下错误:

error[E0433]: failed to resolve. Did you mean `keyboard::sdl2`?
 --> src/keyboard.rs:4:5
  |
4 | use sdl2::keyboard::Keycode;
  |     ^^^^ Did you mean `keyboard::sdl2`?

error[E0433]: failed to resolve. Did you mean `keyboard::sdl2`?

我做错了什么?甚至可以在main.rs 文件之外的其他文件中加载外部板条箱吗?

编辑 1:

extern crate sdl2; 添加到我的main.rs 并没有解决问题

【问题讨论】:

  • 您需要在 main.rs 中添加一个extern crate sd12。所有外部依赖项都需要在顶层链接。
  • 错误依旧
  • 我无法重现您的问题:/ 您需要向我们提供更多信息:您是如何编译的?一个简单的cargo build 还是别的什么?另外:你使用的是什么 Rust 版本 (rustc -V)?并请给我们main.rs的全部内容(至少是重现问题所必需的所有部分)。

标签: rust rust-cargo


【解决方案1】:

如上所述,我需要在根目录中声明外部依赖项。当使用lib.rs 文件时,它必须写在那里:

lib.rs:

extern crate sdl2;
//...

为了在 keyboard.rs文件:

use sdl2::keyboard::Scancode;
use sdl2;
// example code below
fn is_a_pressed(e: &sdl2::EventPump) -> bool {
    e.keyboard_state().is_scancode_pressed(Scancode::A)
}

【讨论】:

  • 你不应该同时需要 lib 和 main 文件。这可能是你的问题。
  • 您能更详细地解释您的问题和解决方案吗?以目前的形式,这个问答不太可能帮助任何未来的访客。我们不喜欢这里的 SO(我们的使命就是为未来的访问者建立一个知识库)。通过编辑您的问题和答案,您可以改善这种情况并将知识回馈给社区。​​span>
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多