rust - 尝试通过 cargo 修复更新版本时找不到本地 crate

标签 rust rust-crates rust-bindgen bindgen

上下文:
我有一个名为“libmaths”的本地 C 库,然后使用 Bindgen 创建一个“libmaths-sys”包,该包本地存储在与我的项目相同的目录中。

问题:
我想使用 Rust 2021 版中的一些功能,目前我的项目是在 2018 年构建的。我正在尝试按照以下说明更新项目:

https://doc.rust-lang.org/cargo/commands/cargo-fix.html

Run cargo fix --edition. Consider also using the --all-features flag if your project has multiple features. You may also want to run cargo fix --edition multiple times with different --target flags if your project has platform-specific code gated by cfg attributes.

Modify Cargo.toml to set the edition field to the new edition.

Run your project tests to verify that everything still works. If new warnings are issued, you may want to consider running cargo fix again (without the --edition flag) to apply any suggestions given by the compiler.

要运行cargo fix --edition,编译器告诉我删除cargo toml中的版本=“2018”。之后我收到一条编译错误,指出找不到 libmaths-sys。该代码在 2018 年可以正常编译和执行,但不能没有此版本标签。

我找不到任何有类似问题的人,这是我的第一个 stackoverflow 问题,因此不确定如何最好地展示我的代码,因为它是一个小项目的上下文。

错误代码

error[E0432]: unresolved import `libmaths_sys`

 --> src/main.rs:1:5
  |
1 | use libmaths_sys::*; // lib.rs in sys crate
  |     ^^^^^^^^^^^^ maybe a missing crate `libmaths_sys`?

项目的文件结构和总体概述

.
├── Cargo.lock
├── Cargo.toml
├── libmaths
│   ├── add.c
│   ├── add.h
│   └── subtract.c
├── libmaths-sys
│   ├── build.rs
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├── src
│   │   └── lib.rs
│   └── wrapper.h
├── README.md
└── src
    ├── lib.rs
    └── main.rs

libmaths 包含返回 a + b 的 add.c 和返回 a - b 的 minus.c,头文件 add.h 指向两个 .c 文件

bindgen 生成的 Rust 代码通过 libmath-sys 箱中的 lib.rs 附加,该箱链接到 OUT DIR,我从树中省略了该目录以保存 200 行文件名。

最佳答案

尝试将 edition="2018" 更新为 edition="2021";否则it defaults to edition="2015"其中requires usage of extern crate .

关于rust - 尝试通过 cargo 修复更新版本时找不到本地 crate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71323624/

相关文章:

rust - 当借用在方法调用之后时如何借用两个不相交的字段?

c++ - 如何在 Rust 中静态链接 node.js?

rust - 使用 Rust bindgen 链接头文件的问题

c - Rust bindgen无法找到平台特定的库?

rust - 如何在不实例化的情况下在 Rust 中获取结构字段的大小

rust - 如何从特征实现中返回 HashMap 键的迭代器?

rust - 对 "Rust 🦀 and WebAssembly 🕸"网站上的教程感到困惑

unit-testing - 测试自定义箱子

module - 我可以在不为每个文件引入模块的情况下将 crate 拆分为多个文件吗?