Bindgen tutorial - problem with OUT_DIR not set


i'm trying to get some C library integrated, and have been following bindgen tutorial at:

However, doing all the previous steps, i stop at the error (removed some empty lines to save space):

# cargo build --verbose
error: environment variable `OUT_DIR` not defined
 --> src/
5 | include!(concat!(env!("OUT_DIR"), "/"));
  |                  ^^^^^^^^^^^^^^^
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
error: could not compile `rust-demo-lib`.
Caused by:
  process didn't exit successfully: `rustc --crate-name rust_demo_lib --edition=2018 src/ --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=d74e1daf97a74d57 -C extra-filename=-d74e1daf97a74d57 --out-dir /.../target/debug/deps -C incremental=/.../target/debug/incremental -L dependency=/.../target/debug/deps` (exit code: 1)

(... in verbose paths above is placeholder for some long path in local filesystem and is valid existing directory)

Is there some relation between the "OUT_DIR" in include! macro, and the "--out-dir" that seem to be set in verbose rustc command?

According to, the variable should be set by cargo automatically? Is there something deprecated in the tutorial, or some parts i may be missing?


Did you create a build script? It's only set when a build script exists.

assuming that you mean, yes, it is located as instructed in tutorial...

and your question helped me to realize terrible, terrible mistake:

  • "root" of crate is NOT the src directory...
  • i mistakenly had src/, as src is the place where all the other .rs files are...
    (correlation -> incorrect assumption -> bug);
  • moving file one directory up resolved the problem...

now it seems to build all the dependencies etc., and seems to invoke what is needed;

thank you!


You're welcome!

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.