Environment variable `OUT_DIR` not defined


#1

Hi all,

I am getting a compile error in my rust program stating that the env variable OUT_DIR is not defined. To reproduce, all I am doing is creating a new cargo bin app and using the env!("OUT_DIR") macro in the main fn.

Is this intended behaviour? If so how would I go about bringing in code that gets generated at compile time?

Thanks


#2

It looks like Cargo won’t pass OUT_DIR to the compiler unless there is a build script defined.

Here’s a minimal example that works for me:

==> src/main.rs <==
fn main() {
    println!("Hello, world!, {}", env!("OUT_DIR"));
}

==> build.rs <==
fn main() { }

==> Cargo.toml <==
[package]
name = "foo"
version = "0.1.0"
build = "build.rs"

#3

Ah, ok then. I am pulling in a local crate that has a build script and I was hoping to grab hold of the generated code in the app that is using that crate.

Based on that I will just add a local build script to my app which should solve the issue.