Reputation: 1309
I have a binary crate that depends on a local library
I'm trying to call the paralang::hello_world from main.rs of the binary crate.
library Cargo.toml
[package]
name = "paralang"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
binary 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" }
The file system looks like this:
D:\Repos\paralang (library crate)
D:\Repos\paralang-language-server (binary crate)
I added the local dependency with cargo add --path "../paralang"
The compiler manages to resolve the hello_world function, but complains about the 'paralang' module.
When running cargo build I get this error:
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`.
Additional context:
rustc 1.70.0 (90c541806 2023-05-31)
cargo 1.70.0 (ec8a8a0ca 2023-04-25)
What am I doing wrong?
Upvotes: 4
Views: 1990
Reputation: 27419
The problem here isn't that the paralang
crate is local, but that a crate with the only type cdylib
does not produce a Rust library which can be imported with extern crate cratename;
or the cargo
magic, only1:
--crate-type=cdylib
,#![crate_type = "cdylib"]
- A dynamic system library will be produced. This is used when compiling a dynamic library to be loaded from another language. This output type will create*.so
files on Linux,*.dylib
files on macOS, and*.dll
files on Windows.
To support both a C-FFI dynamic library and including from other Rust crates simply add both cdylib
and lib
to your crate-type
:
[lib]
crate-type = ["cdylib", "lib"]
Which will cause that:
--crate-type=lib
,#![crate_type = "lib"]
- A Rust library will be produced. This is an ambiguous concept as to what exactly is produced because a library can manifest itself in several forms. The purpose of this generic lib option is to generate the "compiler recommended" style of library. The output library will always be usable by rustc, but the actual type of library may change from time-to-time. The remaining output types are all different flavors of libraries, and the lib type can be seen as an alias for one of them (but the actual one is compiler-defined).
in addition.
See also Dynamic linking between crates for how to dynamically link instead of statically with lib
.
1: from Linkage – The Rust Reference
Upvotes: 8