简体   繁体   English

rust 带货物的库 (rlib)

[英]rust libraries with cargo (rlib)

I am trying to create a library in rust to be used with rust executables.我正在尝试在 rust 中创建一个库,以与 rust 可执行文件一起使用。 In C you can just create your.a or.so (or.lib or.dll on windows) and use tools like CMake to link everything, however rust does not seem to have this kind of infrastructure?在 C 中,您可以创建您的.a 或.so(或.lib 或.dll 在 Windows 上)并使用 CMake 之类的工具来链接所有内容,但是 rust 似乎没有这种基础设施?

It is possible to make an executable with cargo (cargo new ) and create a library by adding the --lib flag (cargo new --lib), but then how would you use the resulting.rlib file (from the library cargo project)?可以使用 cargo (cargo new) 创建一个可执行文件并通过添加 --lib 标志 (cargo new --lib) 创建一个库,但是你将如何使用生成的 .rlib 文件(来自库 cargo 项目) ? I managed to link the.rlib file as follows:我设法按如下方式链接 .rlib 文件:

rustc main.rs --extern foo=libfoo.rlib

and that works beautifully, though, I am not interested in writing a thousand rustc commands to build the final executable (which depends on the.rlib) if there is cargo that can do that for you.并且效果很好,不过,如果有货物可以为您完成,我对编写一千个 rustc 命令来构建最终的可执行文件(这取决于 .rlib)不感兴趣。 I tried working with a build script (which works perfectly for any C library, static or dynamic), but if I try it with the.rlib file, cargo says that it cannot find "foo" (-lfoo), the build script:我尝试使用构建脚本(它适用于任何 C 库、static 或动态库),但如果我尝试使用 .rlib 文件,cargo 说它找不到“foo”(-lfoo),构建脚本:

fn main() {
  println!("cargo:rustc-link-search=.");
  println!("cargo:rustc-link-lib=foo");
}

I tried replacing the path (search) to different directories (whilst also moving the.rlib file to the correct directory), also tried different combinations of libfoo, libfoo.rlib, ... (note that for the C libaries, foo is sufficient).我尝试将路径(搜索)替换为不同的目录(同时也将 .rlib 文件移动到正确的目录),还尝试了 libfoo、libfoo.rlib、...的不同组合(请注意,对于 C 库,foo 就足够了).

So my question really is: How can you create a rust library for private use, and how do you use it with a rust executable in a proper way, avoiding manual rustc commands?所以我的问题实际上是:如何创建供私人使用的 rust 库,以及如何以正确的方式将它与 rust 可执行文件一起使用,避免手动 rustc 命令? Are there tools that do this?有没有工具可以做到这一点? Am I missing something in the build script?我在构建脚本中遗漏了什么吗? Perhaps there exists something like CMake for rust?也许对于 rust 存在类似 CMake 的东西?

I suppose it is possible to just create a C interface over the rust code and compile another C project as that does work with cargo.我想可以在 rust 代码上创建一个 C 接口并编译另一个 C 项目,因为它确实适用于货物。

I do NOT want to publish the code to crates.io as I want this library strictly for private use.我不想将代码发布到 crates.io,因为我希望这个库严格供私人使用。

Cargo does not support using pre-compiled .rlib s. Cargo 不支持使用预编译的.rlib Cargo is designed to compile programs fully from source (not counting native libraries). Cargo 旨在完全从源代码编译程序(不包括本地库)。

How can you create a rust library for private use … I do NOT want to publish the code to crates.io as I want this library strictly for private use.您如何创建供私人使用的 rust 库……我不想将代码发布到 crates.io,因为我希望这个库严格供私人使用。

To use a private library, you write a dependency using a path or git dependency (or use a private package registry, but that's more work to set up).要使用私有库,您可以使用pathgit依赖项编写依赖项(或使用私有 package 注册表,但这需要更多的设置工作)。

[dependencies]
my-lib-a = { path = "../my-lib-a/" }
my-lib-b = { git = "https://my-git-host.example/my-lib-b", branch = "stable" }

Your private library is now compiled exactly like a “public” one.您的私人图书馆现在的编译方式与“公共”图书馆完全一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM