My main.rs
looks like
// #[link(name = "lib")]
extern "C" {
fn hello();
}
fn main() {
unsafe {
hello();
}
}
And lib.rs
:
#[no_mangle]
pub fn hello() {
println!("Hello, World!");
}
I have compiled lib.rs
using rustc --crate-type=cdylib lib.rs -o lib.so
How do I link lib.so
to rustc main.rs
command ?
You need to match ABIs. When you use an
extern "C"
block, you need to declare your functions using the same ABI.Name your dynamic library using the platform's conventions. Use
.dylib
on macOS,.lib
on Windows, and.so
on Linux.rustc
will do this for you automatically if you don't provide a-o
option.Once you have built your dynamic library, you need to add it to the compiler's linker options.
rustc --help
has a list of the various compiler options.-L
adds a directory to the search path and-l
links to a specific library.lib.rs
main.rs
Compiled and executed with:
As I'm on macOS, I used
otool
to show that it's indeed dynamically linked:See also:
For completeness, here's "normal" linking of crates:
lib.rs
main.rs