C++ binding problem: fatal error: 'stdlib.h' file not found

Hi,

I am trying to learn how to bind some cpp/hpp sources with Rust code. Most c tutorials work out of the box but this little my hpp example throws errors:

test.hpp

#include <vector>
#include <numeric>
#include <iostream>


// std::vector, std::accumulate, std::cout, std::endl
using namespace std;

int sum(int a, const int b) {
    cout << "sum(int, const int)" << endl;
    const int c = a + b;
    ++a; // Possible, not const
    return c;
}

float sum(const float a, float b) {
    cout << "sum(const float, float)" << endl;
    return a + b;
}

int sum(vector<int> v) {
    cout << "sum(vector<int>)" << endl;
    return accumulate(v.begin(), v.end(), 0);
}

float sum(const vector<float> v) {
    cout << "sum(const vector<float>)" << endl;
    return accumulate(v.begin(), v.end(), 0.0f);
}

Error:

   Compiling bind v0.1.0 (/home/projects/RustSandbox/bind)
error: failed to run custom build command for `bind v0.1.0 (/home/projects/RustSandbox/bind)`

Caused by:
  process didn't exit successfully: `/home/projects/RustSandbox/bind/target/debug/build/bind-1cea09db913acffc/build-script-build` (exit code: 101)
--- stdout
cargo:rustc-link-lib=test
cargo:warning=couldn't execute `llvm-config --prefix` (error: No such file or directory (os error 2))
cargo:warning=set the LLVM_CONFIG_PATH environment variable to a valid `llvm-config` executable

--- stderr
/usr/lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/cstdlib:75:15: fatal error: 'stdlib.h' file not found
/usr/lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/cstdlib:75:15: fatal error: 'stdlib.h' file not found, err: true
thread 'main' panicked at 'Unable to generate bindings: ()', src/libcore/result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

I am using a classic builder:


extern crate bindgen;

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

fn main() {
    println!("cargo:rustc-link-lib=test");

    let bindings = bindgen::Builder::default()
        .header("test.hpp")
        .generate()
        .expect("Unable to generate bindings");

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

So my first problem is : How the fix the above error?

My second problem: is it possible to split hpp into hpp and cpp so that only function signatures are in the header and the actual functions are in cpp? and how do I then automatically create library object file (test.o and test.hpp) and bind it?

thank you

One more update if it helps:

// If I run : 
bindgen test.hpp -- -x c++

// i get :
[2019-12-20T10:45:35Z ERROR bindgen::ir::item] Unhandled cursor kind 400: Cursor( kind: UnexposedAttr, loc: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/x86_64-linux-gnu/c++/7.4.0/bits/c++config.h:253:43, usr: None)
[2019-12-20T10:45:35Z ERROR bindgen::ir::item] Unhandled cursor kind 400: Cursor( kind: UnexposedAttr, loc: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/x86_64-linux-gnu/c++/7.4.0/bits/c++config.h:257:43, usr: None)
[2019-12-20T10:45:35Z ERROR bindgen::ir::item] Unhandled cursor kind 417: Cursor(default kind: attribute(visibility), loc: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/functexcept.h:42:15, usr: None)
...

It sounds like an llvm-config executable is missing. Try to install it, or specify the path if it's already installed?

1 Like

It is not a problem with llvm-config or anything about llvm, clang or stdlib.h. These are two bugs in bindgen:

  1. .hpp will be handled incorrectly by bindgen, and throw tons of seemingly impossible clang errors if you don't add -- -x c++ to the arguments. Running with -- -x c++ is in this case correct and absolutely required.

  2. Unhandled cursor kind means one of the headers has used a C++ feature that bindgen simply doesn't support, and won't be able to generate code for it. Try avoiding the problematic header, commenting out some includes, or maybe try adding -std=c++98 or something like that to arguments to disable any non-trivial features.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.