How do I split my code into library and binary parts when the library is a `staticlib`?
⚓ Rust 📅 2025-05-25 👤 surdeus 👁️ 11The following compiles fine:
[package]
name = "foo"
version = "0.1.0"
edition = "2024"
[dependencies]
[lib]
name = "foo"
path = "lib.rs"
[[bin]]
name = "bar"
path = "main.rs"
// main.rs
use foo::test;
fn main () {
test();
println!("Yay!");
}
// lib.rs
pub fn test() {}
However, when I add the key crate-type = ["staticlib"] to the [lib] table in Cargo.toml, I get:
error[E0432]: unresolved import `foo`
--> main.rs:1:5
|
1 | use foo::test;
| ^^^ use of undeclared crate or module `foo`
For more information about this error, try `rustc --explain E0432`.
error: could not compile `foo` (bin "bar") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
Why exactly is Cargo getting upset here? I'd expect not much to be different at the codegen stage, and when linking it could still find the necessary objects in the static library. Then again, I'm nowhere near an expert on Rust.
4 posts - 2 participants
🏷️ rust_feed