nagisa / rust_libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.

Home Page:https://docs.rs/libloading

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`"symbol not found"` error

ra0x3 opened this issue · comments

Description

  • When trying to follow the docs at https://docs.rs/libloading/latest/libloading/, I'm currently getting the following error when trying to compile a simple foobar example.
  • I feel like I'm making a simple/easy mistake but not sure how as I'm following the docs pretty closely

Error

thread 'main' panicked at 'bar: DlSym { desc: "dlsym(0x21d897740, foobar): symbol not found" }', bar/src/main.rs:4:83
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Minimum reproducible example

Project layout

.
├── Cargo.lock
├── Cargo.toml
├── bar
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── foo
    ├── Cargo.lock
    ├── Cargo.toml
    └── src
        └── lib.rs

4 directories, 7 files

My fake library who's function I'd like to call

// foo/src/lib.rs
fn foobar() {
    println!("Hello world!");
}

My fake binary, in which I'm trying to load my foo library and call it's foobar function

// bar/src/main.rs
fn main() {
        unsafe {
            let lib = libloading::Library::new("/path/to/my/libfoo.dylib").expect("foo");
            let func: libloading::Symbol<unsafe extern fn()> = lib.get(b"foobar").expect("bar");
            func();
        }
}

Expected behavior

  • I'd expect the foobar function in foo/src/lib.rs to get called, thus printing "Hello world!"

Rust does not export functions by default (you need to make them public!) and it also mangles functions by default (see the no_mangle attribute – documentation references this problem here). You’re also missing extern for your function definition to make sure that the ABI matches on both sides. This chapter from nomicon might be helpful to you.

libfoo.dylib suggests me you’re on MacOS. You should be able to use the nm utility to list the symbols in your library. For a symbol to be importable, it’ll need to have a capital letter (indicating it is an exported global symbol) such as T on the 2nd column.

I strongly recommend seeking advice/help on the venues dedicated to helping Rustaceans – you will be able to get help much faster than from me on this issue tracker.