How to use compiled rust library file(.rlib file)

guys i am new to rust and i am writing my own personal set of math library to make job easy , have lot of work that requires me to play with math (kind of lazy shortcuts, i used to have them in C as well )

i want to make use of compiled library (that single .rlib file) , because i have to share it with my friends (and i don’t want distribute it in source form , because they will try being cool and mess up the project at their end and i will have to waste days finding the issue )

Rust’s rlib does not have a stable ABI, so generally it isn’t used by anyone outside of Rust compiler’s own internals.

To distribute compiled code you need to write a C interface for it, and build cdylib crate type, and then have users link it like a C library. It is a bit of a hassle to do that.

The easy ways of sharing:

  • if it’s just in your project, use workspaces. In Cargo.toml add [dependencies] your_helper = {path="../your_helper"} and it will make extern crate your_helper; available if there’s ../your_helper/Cargo.toml for the other project.

  • if you want to share it just between friends or colleagues, use git: In Cargo.toml add [dependencies] your_helper = { git = "" }

  • if you want to share it with everyone, publish it on cargo publish

All these methods expose the source code, but they don’t allow others to modify it without your permission.

1 Like