Clever system !
Clever system !
Posted Dec 5, 2024 7:31 UTC (Thu) by samlh (subscriber, #56788)In reply to: Clever system ! by Karellen
Parent article: Rust's incremental compiler architecture
[...]what is the difference between a crate and a library?To keep it short:
- A crate is the rust unit of compilation, composed of 1 or more rust source files
- Crates can be compiled as libraries or executables
- Library crates get compiled to a
.rlib
file which contains compiled binary code along with metadata for:- exported apis, static variables, constants, etc
- generic function implementations (in a partially compiled form)
- other stuff :) [for example, when LTO is involved, the rlib can contain LLVM bitcode]
- Think of .rlibs as equivalent to a C++ static library + header files + linking instructions.
- The .rlib format (and the Rust abi more generally) is not stable across compiler releases, so .rlib files are usually treated as temporary artifacts (similar to .o files).
- When compiling the final executable, the binary code from the rlibs are statically linked together, along with instantiations of the imported generic functions (as needed).
- It is also possible to compile crates as C-style static libraries (libfoo.a) or shared objects (libfoo.so). This is often done in order to expose a stable C-compatible abi to be used externally.
See Rust by example and the Rust book for more info.