[英]How can I use Cargo to build a library as both an rlib and a dylib but with different contents?
我想做一个包含以下内容的项目:
目录结构,不包括临时文件和其他垃圾:
.
├── Cargo.toml
├── src
│ ├── c_bindings.rs // contains C bindings for library
│ ├── compression.rs
│ ├── const_data.rs
│ ├── hash.rs
│ ├── lib.rs // library
│ └── main.rs // simple executable that uses library
└── target
└── debug
├── gost_stribog
├── libgost_stribog.rlib
我希望cargo build
做到这一点:
c_bindings.rs
Rust 库(rlib)c_bindings.rs
C 库(dylib)调试目录应该是:
└── target
└── debug
├── gost_stribog
├── libgost_stribog.rlib
├── libgost_stribog.so
我的Cargo.toml
应该是什么样的?
意见回答: 不要 。
相反,将代码拆分为两个或三个单独的包:
然后,将您的c_bindings.rs
移动到绑定包只作为lib.rs
它可以依赖于核心库。 您还可以将main.rs
移动到另一个依赖核心库的包中。
这三个包可以位于相同的源代码存储库中,但是将使用单独的调用构建。
货运工作空间可能证明是有用的; 实际上它被列为一个明确的原因(“FFI包装箱用于FFI绑定的子箱”)。
事实上,你可以。 这是如何。
$PROJECT_ROOT/.cargo/config.rs
[build]
rustflags = ["-C", "prefer-dynamic"]
请记住,您还需要使用 Rust 标准库分发您的应用程序。
$ ls $(rustc --print=sysroot)/lib
libLLVM-13-rust-1.57.0-beta.so librustc_driver-3aebdc12af579500.so libstd-c8bc39dac3997df6.so libtest-4d997b51b1a49b1f.so rustlib
在我的例子中,它被命名为 libstd-c8bc39dac3997df6.so,因为我在 linux 上,如果你在 windows 上,它应该在 bin 文件夹而不是 lib 文件夹中。
此外,您还需要一个脚本来在 linux 和 MacOS 上运行该程序。 在 linux 和 macos 上,您不只是将库放在与应用程序相同的目录中并期望它能够工作,而是有 2 个选项。
/usr/lib
文件夹中,然后运行它。exec LD_LIBRARY_PATH=$PWD ./YOU_APPLICATION $@ # this last commands tells sh to get the command line arguments.
只是。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.