Unable to use cc crate in build.rs

I'm trying to use the cc crate to run a simple hello world c FFI as described in Build Script Examples - The Cargo Book

I've added a build.rs at the top of my project with

// Example custom build script.
fn main() {
    // Tell Cargo that if the given file changes, to rerun this build script.
    println!("cargo::rerun-if-changed=src/hello.c");
    // Use the `cc` crate to build a C file and statically link it.
    cc::Build::new()
        .file("src/hello.c")
        .compile("hello");
}

and I've added cc as a dependency using cargo add cc. However each time I try cargo build, i get the error

...
   Compiling cc v1.2.18
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cc`
 --> build.rs:6:5
  |
6 |     cc::Build::new()
  |     ^^ use of unresolved module or unlinked crate `cc`
  |
  = help: if you wanted to use a crate named `cc`, use `cargo add cc` to add it to your `Cargo.toml`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `example` (build script) due to 1 previous error

Dependencies used by build.rs belong in the [build-dependencies] table, not in [dependencies]. Using cargo add, you can do this with cargo add --build.

1 Like

Ahhhhh thanks, I didn't look carefully enough to notice I needed cc under [build-dependencies] and not just [dependencies]