rust-lang / rust-lang/rust-bindgen

wrap_static_fns is missing from bindgen::Builder

Open
#2,959 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi, I think I'm going crazy, I have been trying to use an external C library in my Rust program without success.
The C library is uhubctl, source code is here https://github.com/mvp/uhubctl/tree/master

As you can see most of the important functions are declared as static functions, for instance

static int usb_find_hubs(void)

And I need to expose them to my Rust program.

After struggling for a while, I realise that the bindings.rs file generated by Bindgen doesn't contain anything, and I also realised that I need the wrap_static_fns flag. Allegedly it should be a method in bindgen::Builder.

However, my VSCode keeps saying that the method is not found, and cargo build gives the same result.

error[E0599]: no method named `wrap_static_fns` found for struct `bindgen::Builder` in the current scope

If I run the command line version

bindgen libs/uhubctl.h -o src/bindings.rs -- -I//opt/homebrew/Cellar/libusb/1.0.27/include/libusb-1.0 -DPROGRAM_VERSION=\"2.6.0\" --wrap-static-fns

I get

panicked at /Users/hoang.bui/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-cli-0.70.1/main.rs:45:36:
Unable to generate bindings: ClangDiagnostic("error: unsupported option '--wrap-static-fns'\n")

I am also a total beginner in Rust so I'm completely lost.

I am on macOS Sonoma 14.6.1,
rustc 1.82.0
bindgen 0.70.1

Here is my build.rs

/* File: build.rs */
extern crate bindgen;
extern crate cc;
use std::path::PathBuf;

fn main() {
    // Tell cargo to invalidate the built crate whenever the wrapper changes
    println!("cargo:rerun-if-changed=libs/uhubctl.c");

    let libusb_header_path_flag = "-I/opt/homebrew/Cellar/libusb/1.0.27/include/libusb-1.0";
    let uhubctl_version_flag = "-DPROGRAM_VERSION=\"2.6.0\"";
    
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate bindings for.
        .header("libs/uhubctl.h")
        .wrap_static_fns(true)
        .clang_arg(libusb_header_path_flag)
        .clang_arg(uhubctl_version_flag)
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        // Finish the builder and generate the bindings.
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindings to the src/bindings.rs file.
    let out_path = PathBuf::from("src");
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");

    // Build static library
    cc::Build::new()
        .file("libs/uhubctl.c")
        .flag(libusb_header_path_flag)
        .flag(uhubctl_version_flag)
        .compile("uhubctl.a");
}

Any help would be much 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 with build.rs, the bindgen::Builder call, and the bindgen 0.70.1 command-line invocation shown in the issue. Compare the available Builder methods and CLI options with the documented static-function wrapping behavior, using libs/uhubctl.h and the reported diagnostics; done means the supported usage or API discrepancy is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.