Bindgen tutorial - problem with OUT_DIR not set

Hello,

i'm trying to get some C library integrated, and have been following bindgen tutorial at:
https://rust-lang.github.io/rust-bindgen/tutorial-4.html

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/lib.rs:5:18
5 | include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
  |                  ^^^^^^^^^^^^^^^
  = 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/lib.rs --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 https://doc.rust-lang.org/cargo/reference/environment-variables.html, the variable should be set by cargo automatically? Is there something deprecated in the tutorial, or some parts i may be missing?

thanks,
j.

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

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

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

  • "root" of crate is NOT the src directory...
  • i mistakenly had src/build.rs, as src is the place where all the other .rs files are...
    (correlation -> incorrect assumption -> bug);
  • moving build.rs 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!

2 Likes

You're welcome!