Select a shared or static library using Cargo

I am trying to modify Racer to generate a shared library instead of rlib.

To do this, I added crate-type = ["dylib"] to the [lib] section of the Cargo manifest, and then launched cargo build --lib . This worked fine, and libracer.so was emitted.

Unfortunately, now I could not create the Racer binary, which depends on the static version of the library. Running cargo build complains:

  Compiling racer v1.0.0 (file:///home/georgev/dotfiles/vim/bundle/racer) error: cannot satisfy dependencies so `std` only shows up once help: having upstream crates all available in one format will likely make this go away error: cannot satisfy dependencies so `core` only shows up once help: having upstream crates all available in one format will likely make this go away error: cannot satisfy dependencies so `collections` only shows up once help: having upstream crates all available in one format will likely make this go away error: cannot satisfy dependencies so `rustc_unicode` only shows up once help: having upstream crates all available in one format will likely make this go away error: cannot satisfy dependencies so `alloc` only shows up once help: having upstream crates all available in one format will likely make this go away error: cannot satisfy dependencies so `libc` only shows up once help: having upstream crates all available in one format will likely make this go away error: cannot satisfy dependencies so `rand` only shows up once help: having upstream crates all available in one format will likely make this go away error: aborting due to 7 previous errors Could not compile `racer`. 

I changed crate-type to ["dylib", "bin"] , which allowed the compilation to succeed. However, cargo build --lib will no longer generate a shared library (rlib only).

How can I indicate which type of library I would like to build while maintaining a static library for inclusion in the executable?

+5
source share
1 answer

bin not a valid crate-type value. Valid values ​​are rlib , lib , staticlib and dylib . Change box type to

 crate-type = ["dylib", "rlib"] 

will lead to the behavior that you are after.

The reason that only rlib is emitted using ["dylib", "bin"] is because there is currently a Cargo error that causes invalid values ​​for crate-type to create only rlib. I fixed the problem to fix the problem.

+6
source

All Articles