Linking cdylib and bin within same package

I have a rust project that I am compiling for by rpi4. The pkg produces 2 bin and 1 lib (rust allows only 1 lib). The Cargo.toml for the project looks like the following:

[package]
name = "rpi4_cross_compile"
version = "0.1.0"
edition = "2021"
links = "atomic"
default-run = "rust_hello_world_elf"

[[bin]]
name = "rust_atomic_elf"
path = "src/bin/atomic.rs"

[[bin]]
name = "rust_hello_world_elf"
path  = "src/bin/hello_world.rs"

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

[dependencies]
libc = "0.2.155"

How do I access the foo lib inside my 2 bin crates and link against the so file produced by the lib ?
The cdylib exports a simple function onload for testing purposes. Is this even possible ? I do not want to export lib as a "rlib" type. How does one consume a cdylib produced by a pkg within the same pkg for bin/test/benchmark crates/purposes ?

1 Like

You need either a second package or to use crate-type = ["cdylib", "rlib"].

To link against a cdylib you have to treat it like foreign code — make a second package and add linking options via the build script.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.