Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fatal error: 'stdlib.h' file not found when including cstdlib header #1919

Closed
doppioandante opened this issue Nov 16, 2020 · 3 comments · Fixed by #1933
Closed

fatal error: 'stdlib.h' file not found when including cstdlib header #1919

doppioandante opened this issue Nov 16, 2020 · 3 comments · Fixed by #1933

Comments

@doppioandante
Copy link

doppioandante commented Nov 16, 2020

There are similar issues (e.g. #1521) but I can't understand how to fix this. I don't think this is a bindgen issue, but can't unterstand what underlying component is causing this. My system is Ubuntu 20.10, with llvm & co installed.

Repro: Modify https://github.com/fitzgen/bindgen-tutorial-bzip2-sys.git like this:

rename wrapper.h to wrapper.hpp

wrapper.hpp

#include <bzlip.h>
#include <cstdlib>

and modify build.rs:

    println!("cargo:rustc-link-lib=bz2");

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

The bzip.h include is useless but I wanted to start from a basilar working repo.
Substituting #include <cstdlib> with #include <stdlib.h> makes bindgen happy.

cargo build result

$ RUST_BACKTRACE=1 cargo build
   Compiling bindgen-tutorial-bzip2-sys v0.1.0 (/home/enrico/projects/misc/bindgen-tutorial-bzip2-sys)
error: failed to run custom build command for `bindgen-tutorial-bzip2-sys v0.1.0 (/home/enrico/projects/misc/bindgen-tutorial-bzip2-sys)`

Caused by:
  process didn't exit successfully: `/home/enrico/projects/misc/bindgen-tutorial-bzip2-sys/target/debug/build/bindgen-tutorial-bzip2-sys-345f7e1f733377eb/build-script-build` (exit code: 101)
  --- stdout
  cargo:rustc-link-lib=bz2

  --- stderr
  /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/cstdlib:75:15: fatal error: 'stdlib.h' file not found
  /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/cstdlib:75:15: fatal error: 'stdlib.h' file not found, err: true
  thread 'main' panicked at 'Unable to generate bindings: ()', build.rs:13:10
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/std/src/panicking.rs:475
     1: core::panicking::panic_fmt
               at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/panicking.rs:85
     2: core::option::expect_none_failed
               at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/option.rs:1221
     3: core::result::Result<T,E>::expect
               at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/result.rs:933
     4: build_script_build::main
               at ./build.rs:9
     5: core::ops::function::FnOnce::call_once
               at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/ops/function.rs:227
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@emilio
Copy link
Contributor

emilio commented Nov 25, 2020

<cstdlib.h> is not a valid header. If you're building in C mode you need <stdlib.h>, if you're building in C++ mode you need <cstdlib> (note: no .h).

That header doesn't compile in a regular C compiler either, so I think the behavior here is right, but please reopen if I've missed something.

@emilio emilio closed this as completed Nov 25, 2020
@doppioandante
Copy link
Author

doppioandante commented Nov 25, 2020

I'm sorry, I obviously mistyped. The repro file correctly has <cstdlib> and I edited the initial post. Could you please reopen @emilio?

@emilio emilio reopened this Nov 25, 2020
emilio added a commit that referenced this issue Nov 26, 2020
Fixes #1919, as otherwise we peek the wrong include paths unless you
pass "-x c++" or such.
@emilio
Copy link
Contributor

emilio commented Nov 26, 2020

Ah, I see what's going on... Right now bindgen assumes C input unless -x c++ is specified (or similar).

I opened #1933 to also look at the filename.

emilio added a commit that referenced this issue Nov 26, 2020
Fixes #1919, as otherwise we peek the wrong include paths unless you
pass "-x c++" or such.
LoganBarnett pushed a commit to LoganBarnett/rust-bindgen that referenced this issue Dec 2, 2023
Fixes rust-lang#1919, as otherwise we peek the wrong include paths unless you
pass "-x c++" or such.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants