Charles Carriere
Charles Carriere

Reputation: 389

Why do I get "can't find crate" that is listed as a dependency in Cargo.toml when I compile with rustc?

My Cargo.toml includes this:

[dependencies]
chrono = "0.4"

And my code includes this:

extern crate chrono;
use chrono::{Duration, DateTime, Utc};

yet when I run my code, I get this error:

error[E0463]: can't find crate for `chrono`
 --> src/lib.rs:1:1
  |
1 | extern crate chrono;
  | ^^^^^^^^^^^^^^^^^^^^ can't find crate

I am working on an Exercism exercise, so the way I am building/running the program is rustc src/lib.rs to test my solution. Is the problem because I'm not running rustc src/main.rs?

Upvotes: 8

Views: 3890

Answers (1)

Cerberus
Cerberus

Reputation: 10218

When you're directly running rustc, all that compiler knows is the command line arguments. It doesn't know anything about Cargo.toml, in particular, and so it doesn't know where to look for chrono library.

To use dependency management, you have to compile your project with Cargo - just use cargo build/cargo run/cargo test, and everything should be fine. See the Book for details.

If, however, you want (for some reason) use rustc directly, I'd advise to check first cargo anyway, by using cargo build --verbose. It will show all the commands that are invoked, allowing you to inspect the possible arguments to be defined manually.

Upvotes: 9

Related Questions