Some question when using bindgen to binding kernel c header file

wrapper.h

#include<notifier.h>
#include<oom.h>

build.rs

use std::env;
use std::path::PathBuf;

fn main() {
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("wrapper.h")
        .clang_arg("-I/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include")
        .clang_arg("-I/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/arch/x86/include")
        .clang_arg("-I/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/arch/x86/include/generated")
        .clang_arg("-I/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux")
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

result

error: failed to run custom build command for `oom_monitor_lib v0.1.0 (/root/oom_monitor_lib)`

Caused by:
  process didn't exit successfully: `/root/oom_monitor_lib/target/debug/build/oom_monitor_lib-509aa168312a250f/build-script-build` (exit status: 101)
  --- stdout
  cargo:rustc-link-search=/path/to/lib
  cargo:rustc-link-lib=bz2
  cargo:rerun-if-changed=wrapper.h

  --- stderr
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/compiler_attributes.h:66:9: warning: '__always_inline' macro redefined [-Wmacro-redefined]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/uapi/linux/stddef.h:5:9: note: previous definition is here
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/asm-generic/rwonce.h:64:8: error: unknown type name '__no_sanitize_or_inline'
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/asm-generic/rwonce.h:82:8: error: unknown type name '__no_kasan_or_inline'
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/arch/x86/include/asm/bitops.h:87:2: warning: implicit declaration of function 'barrier' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:69:10: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:75:26: warning: implicit declaration of function 'hweight32' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:75:41: warning: implicit declaration of function 'hweight64' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:185:10: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:186:9: warning: implicit declaration of function 'fls64' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:194:9: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:239:3: warning: implicit declaration of function 'set_bit' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:241:3: warning: implicit declaration of function 'clear_bit' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:248:3: warning: implicit declaration of function '__set_bit' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:250:3: warning: implicit declaration of function '__clear_bit' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/log2.h:24:9: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/log2.h:32:9: warning: implicit declaration of function 'fls64' is invalid in C99 [-Wimplicit-function-declaration]
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:346:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:347:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:349:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:350:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:367:8: error: 'inline' can only appear on functions
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:367:19: error: static declaration of '__must_check' follows non-static declaration
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:350:5: note: previous definition is here
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:367:31: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:409:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:411:8: error: 'inline' can only appear on functions
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:411:19: error: static declaration of '__must_check' follows non-static declaration
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:409:5: note: previous definition is here
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:411:31: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:432:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:433:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:434:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:435:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:437:17: error: expected ';' after top level declarator
  /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:438:17: error: expected ';' after top level declarator
  fatal error: too many errors emitted, stopping now [-ferror-limit=]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/compiler_attributes.h:66:9: warning: '__always_inline' macro redefined [-Wmacro-redefined]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/arch/x86/include/asm/bitops.h:87:2: warning: implicit declaration of function 'barrier' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:69:10: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:75:26: warning: implicit declaration of function 'hweight32' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:75:41: warning: implicit declaration of function 'hweight64' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:185:10: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:186:9: warning: implicit declaration of function 'fls64' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:194:9: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:239:3: warning: implicit declaration of function 'set_bit' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:241:3: warning: implicit declaration of function 'clear_bit' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:248:3: warning: implicit declaration of function '__set_bit' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/bitops.h:250:3: warning: implicit declaration of function '__clear_bit' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/log2.h:24:9: warning: implicit declaration of function 'fls' is invalid in C99 [-Wimplicit-function-declaration]
  clang diag: /usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/log2.h:32:9: warning: implicit declaration of function 'fls64' is invalid in C99 [-Wimplicit-function-declaration]
  thread 'main' panicked at 'Unable to generate bindings: ClangDiagnostic("/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/asm-generic/rwonce.h:64:8: error: unknown type name '__no_sanitize_or_inline'\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/asm-generic/rwonce.h:82:8: error: unknown type name '__no_kasan_or_inline'\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:346:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:347:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:349:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:350:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:367:8: error: 'inline' can only appear on functions\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:367:19: error: static declaration of '__must_check' follows non-static declaration\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:367:31: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:409:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:411:8: error: 'inline' can only appear on functions\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:411:19: error: static declaration of '__must_check' follows non-static declaration\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:411:31: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:432:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:433:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:434:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:435:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:437:17: error: expected ';' after top level declarator\n/usr/src/kernels/5.10.0-136.49.0.127.u85.fos23.x86_64/include/linux/kernel.h:438:17: error: expected ';' after top level declarator\nfatal error: too many errors emitted, stopping now [-ferror-limit=]\n")', build.rs:32:10
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

you gotta solve those compile errors emitted by clang. bindgen uses libclang to parse the C/C++ definitions in the header files. you must specify the correct compiler flags, just as you would when you build using gcc. if you don't specify the correct compiler flags to properly build the C code, how can bindgen and libclang know them?

try resolve all the compiler errors then bindgen should happily generate ffi bindings for you.

not a kernel developer myself, but some google search, e.g. __no_sanitize_or_inline, shows they are defined in linux/include/linux/compiler_types.h

these errors are not specific to the rust language, nor even to C/C++ in general, but very specific to the linux kernel codebase. the kernel has it's own build system, in general it's not enough to just point a C compiler to a single file in the source tree, even if you can get it to compile, it might not be configured properly to have the correct ABI that's suitable to use on the target system.

let's wait for someone on this forum who has the knowledge, and I also suggest you consult the kernel developer community, as you might have higher chance to get useful help there.

3 Likes

Thank you very much for your help, I will try to find the kernel developers to solve this problem.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.