Import compiled libraries


Is it possible to import/include compiled libraries as we can in C++?

Compiled foreign libraries or compiled Rust libraries?

If the former, this is done all the time; you need to create bindings (analogous to the C header file) and issue linker instructions, which are usually done in a *-sys package.

If the latter: in principle yes, but Cargo does not support this, so you would need to use or create a different build system.

Thank you for answer.
I mean Rust libraries. For example, I built a "example" library and share between projects not sharing source code, like in C++ where I can link compiled library with header file.

So subquestion. Can I link compiled rust libraries to execute program not including it to the single execute program?

So if cargo doesn't support this, do you have any example how to do it?

Can I ask why you want to do this? Rust is designed and optimized for whole program compilation, meaning that the libraries are supplied in source form.

But that's exactly what you are getting when you use library crate like it's described in tutorial!

In fact C++ is on the long road to get where Rust started!

Thus it's very hard to understand what you are asking about.

Curiosity. Sometimes we don't want to share source code etc.

Not precisely. Using this approach I download source code and cargo compiles this. My approach is to download "compiled" library and use it included into executable binary or as separate library linked to executable (similar as I can with C++)

You cannot use a Rust library only by linking it; the compiler must be given the .rlib file produced by compiling the library (or at least .rmeta, which is found inside the .rlib). This is necessary to support generic functions and types, so that the compiler can substitute the appropriate concrete types. You can think of the .rmeta as like an automatically generated header file; like a C++ header containing templates, it contains enough of the original library's code to perform the substitutions (but in MIR rather than text form).

No, I don't use non-Cargo build systems. I only know that it is possible in principle, because rustc only compiles one crate at a time and doesn't know where the input .rlibs came from.

1 Like

@kpreid this is answer for my question, thank you.

Just for completeness, if the issue is source code privacy you can specify cargo dependencies as references to git repos, including private repos.

Yes, I know thank you. My question was mostly scoped to deliver compiled library without source code.

One can load compiled libraries using libloading and trait objects without much pain. Take a look here for some example or research more into this direction.

@itemchenko Thank you very much Ivan

You might also be interested in this RFC RFC: `#[export]` (dynamically linked crates) by m-ou-se · Pull Request #3435 · rust-lang/rfcs · GitHub

Ah, got it. Yeah, Rust is designed to make that hard. Not impossible, mind you, but hard.

Given the problems that C++ got as the result if it's decisions to [try to] establish stable ABI Rust designers pretty consciously decided that this not something they plan to provide.

Note that while certain languages embrace binary compatibiliy and are even built with it in mind (e.g. Java), many others (like Ada) do the same thing Rust does: every release is incompatible with previous one thus you would need to, as minimum, develop a way to recompile and deliver you libraries every 6 weeks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.