选择与Cargo共享或静态库

时间:2015-08-12 22:05:58

标签: shared-libraries rust rust-cargo

我正在尝试修改Racer以发出共享库而不是rlib。

为此,我将crate-type = ["dylib"]添加到货物清单的[lib]部分,然后运行cargo build --lib。这很有效,并且libracer.so被发出。

不幸的是,现在我无法构建Racer二进制文件,这取决于库的静态版本。正在运行cargo build抱怨:

   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`.

我将crate-type更改为["dylib", "bin"],这使得编译成功。但是,cargo build --lib将不再发出共享库(仅限rlib)。

如何指定我想要构建哪种类型的库,同时仍然允许静态构建所述库以包含在可执行文件中?

1 个答案:

答案 0 :(得分:7)

bin不是有效的crate-type值。有效值为rliblibstaticlibdylib。将包类型更改为

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

会导致您的行为。

仅使用["dylib", "bin"]发出rlib的原因是因为当前存在导致crate-type的无效值仅产生rlib的Cargo错误。我已经提交pull request来解决问题。