如何在cargo中声明本地依赖?

问题描述 投票:0回答:1

我有一个依赖于本地库的二进制箱

我正在尝试从二进制板条箱的 main.rs 调用 paralang::hello_world 。

库 Cargo.toml

[package]
name = "paralang"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

二进制 Cargo.toml

[package]
name = "paralang-language-server"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde_json = "1.0.78"
tokio = { version = "1.17.0", features = ["full"] }
tower-lsp = { version = "0.20.0", features = ["proposed"]}
dashmap = "5.5.3"
paralang = { version = "0.1.0", path = "../paralang" }

文件系统如下所示:

D:\Repos\paralang (library crate)
D:\Repos\paralang-language-server (binary crate)

我用

cargo add --path "../paralang"

添加了本地依赖

编译器设法解析 hello_world 函数,但抱怨“paralang”模块。

运行货物构建时出现此错误:

error[E0432]: unresolved import `paralang`
 --> src\main.rs:5:5
  |
5 | use paralang::hello_world;
  |     ^^^^^^^^ use of undeclared crate or module `paralang`

For more information about this error, try `rustc --explain E0432`.

其他背景:

rustc 1.70.0 (90c541806 2023-05-31)
cargo 1.70.0 (ec8a8a0ca 2023-04-25)

我做错了什么?

rust rust-cargo
1个回答
0
投票

具有唯一类型

cdylib
的板条箱不会生成可以使用
extern crate cratename;
cargo
魔法导入的 Rust 库。要支持 C-FFI 动态库并包含其他 Rust 箱,只需将
cdylib
lib
添加到您的
crate-type
:

[lib]
crate-type = ["cdylib", "lib"]
© www.soinside.com 2019 - 2024. All rights reserved.