How to import a box dependency when the library name is different from the package name?

I have a box that is imported directly from GitHub, according to the Cargo documentation :

[dependencies]
libfoo = { git = "ssh://git@github.com/me/libfoo", branch = "dev" }

[lib]
path = "src/rust/lib.rs"
name = "myprj"
crate-type = ["cdylib"]

      

Running cargo build

works great here, Cargo extracts libfoo

and creates it in the directory ~/.cargo

. When I try to use (import) it in lib.rs

:

extern crate libfoo;   //also tried foo

      

Cargo chokes:

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

      

Interestingly, the IntelliJ Rust plugin finds the drawer when I click on it in lib.rs

- it goes to the loaded source in ~/.cargo

...

Depending on the libfoo

section of the lib

file Cargo.toml

is listed as:

[package]
name = "libfoo"
[lib]
name = "foo"
crate-type = ["cdylib"]

      

I've tried all permutations of libfoo and foo to see if Cargo is confused between the lib name and the package / directory name.

It also fails if I specify the local path to the dependency. (Cargo compiles the dependency, but then claims to not find it when it is declared / imported into lib.rs

.)

[dependencies]
libfoo = { path = "/Users/me/dev/libfoo" }

      

If I include a box from git or a filesystem with the same name [lib]

as the name [package]

, it works fine. So the problem is with boxes with a library name ( [lib]

) that is different from the package name ( [package]

).

If I remove the section [lib]

from the dependency file Cargo.toml

it works.

Update : if crate-type = ["cdylib"]

removed from libfoo

, this works with foo

imported. If so, I am getting the same error with extern crate foo;

.

+3


source to share


1 answer


Cargo is interested in package names when it comes to dependencies, whereas the compiler ( rustc

) is interested in library names when it comes to loading their metadata and linking to them.

Let's take another look at this passage Cargo.toml

:

[package]
name = "libfoo"

[lib]
name = "foo"

      

Here is the package libfoo

name and the library name foo

.

If you want to declare a dependency on libfoo

in your project, you need to write the package name ( libfoo

) in the table [dependencies]

. For example:

[dependencies]
libfoo = { git = "ssh://git@github.com/me/libfoo", branch = "dev" }

      

This is what you already have, and rightly so.



However, if you want to import the library into your box, you need to write the name of the library in the item extern crate

, i.e.

extern crate foo;

      


How did I get it? First, I wrote in libfoo

both in Cargo.toml

and in the element extern crate

as you described. When I ran cargo build

, I noticed that it libfoo

was created successfully, which indicates that Cargo has correctly resolved the dependency. But I also noticed that the compiler couldn't find libfoo

how you got around this.

Then I checked the command line passed to rustc

by running cargo build --verbose

. This is what I saw (non-essential parts omitted):

Running `rustc [...] --extern foo=/[path]/target/debug/deps/libfoo-5cf876c5c8ac1bfb.rlib`                                                                 

      

The argument --extern name=path

tells rustc

that the mailbox with the name name

is in path

. The name is here foo

, so we have to write extern crate foo;

in the code to refer to it.

+8


source







All Articles