rust-lang / rust-lang/rust-bindgen

Undefined Symbols upon crate execution

Open
#1,230 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-unclear
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

First issue here, hope I'm following guidelines as closely as possible

Input C/C++ Header
// excerpt from __bindings.i
int c_function(...snip...); // this looks fine
// excerpt from bindings.rs
extern "C" {
    #[link_name = "\u{1}_c_function"]
    pub fn c_function(
        ...snip...
    ) -> ::std::os::raw::c_int;
}
Bindgen Invocation
println!("cargo:rustc-link-search=native={}/{}", cargo_manifest_dir, path_to_native_lib);

bindgen::Builder::default()
    .enable_cxx_namespaces()
    .raw_line("pub use self::root:*;") 
    .header("input.h")
    .generate()
    .expect("Unable to generate API bindings!");

In src/lib.rs:

// generalized...
include!(concat!(env!("OUT_DIR"), "bindings.rs"));

pub fn do_something_with_c_function () {
    ...snip...
    unsafe {
        ...snip...
    }
}
Actual Results

cargo build works, but cargo test with a minimal suite

use super::*;

#[test]
fn test_basic {
    do_something_with_c_function();
}

gives

= note: Undefined symbols for architecture x86_64:
            "_c_function", referenced from:
                my lib mylib::do_something_with_c_function::h653e2de7cade7ada in mylib-054bcda1fe20f5d1.49ritj0qqjv8vn1a.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
Expected Results

The function/symbols appear to actually be present in the {{snip}}.rcgu.o file, but yet can't be linked? The function is also extern "C"'d , so it shouldn't be mangled

Any hints/feedback would be greatly appreciated!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal cargo test described in the issue, then inspect src/lib.rs, the generated bindings.rs, and the cargo rustc-link-search setting. Compare the generated extern symbol and the native library used during cargo build and cargo test; done means the minimal test links and runs successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
build-system, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.