Nodoid
Nodoid

Reputation: 1571

Testing a Rust crate outside of the source package

I have created my Rust crate. It's a very trivial affair. It built fine and when tested within it's own source directory works just fine (I just included extern crate my_first_crate; in my test file).

I want to now test the crate in a totally different application.

If I add the same extern crate line to my new application, the compiler tells me that it can't find the crate. I expected this (I'd get the same in C if I told the compiler to link to a library it has no clue about!)

Do I need to copy the my_first_crate.rlib file from the source to the application target/debug folder or is there a way to tell cargo that it needs to link to the rlib file?

Upvotes: 3

Views: 322

Answers (1)

Francis Gagné
Francis Gagné

Reputation: 65782

You need to add your crate as a dependency for your application. Add this to your application's Cargo.toml:

[dependencies]
my_first_crate = { path = "/path/to/crate" }

"/path/to/crate" is the path to the root of the crate's source (i.e. the directory that contains its Cargo.toml). You can use either a relative or an absolute path (but avoid absolute paths if you intend on publishing your code!).

Upvotes: 4

Related Questions