简体   繁体   中英

How to link a dynamic Rust library using only rustc and not cargo?

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

#[no_mangle]
pub extern "C" fn hello() {
    println!("Hello, World!");
}

main.rs

extern "C" {
    fn hello();
}

fn main() {
    unsafe {
        hello();
    }
}

Compiled and executed with:

$ rustc --crate-type=cdylib lib.rs
$ rustc main.rs -L . -l lib
$ ./main
Hello, World!

As I'm on macOS, I used otool to show that it's indeed dynamically linked:

$ otool -L main
main:
    liblib.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1)
    /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)

See also:


For completeness, here's "normal" linking of crates:

lib.rs

pub fn hello() {
    println!("Hello, World!");
}

main.rs

fn main() {
    lib::hello();
}
$ rustc --crate-type=rlib lib.rs
$ rustc main.rs --extern lib=liblib.rlib
$ ./main
Hello, World!
$ otool -L main
main:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1)
    /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM