What is the Difference Between `dylib` and `cdylib`

OK, here is my current understanding of different Rust library types.

Rlib

rlib's are Rust static libraries. They contain all of the metadata and code necessary to build a Rust crate and link it into another Rust crate statically. Given just the rlib for a crate, you can include that crate into a Rust program by using extern crate crate_name. Even if you are dynamically linking a crate like a .dll, .so, etc., you will still need to have the rlib to include that crate into another crate because they shared library is missing some of the required metadata ( I think ).

Cdylib

cdylib's are primarily designed for building shared libraries that can be linked into C/C++ programs.

  • They have a minimum size of ~2.2M on Linux, so the smallest cdylib is larger than the smallest dylib. This might be because it statically links the standard library.
  • They are designed for building a C ABI that can be dynamically linked to C/C++ programs ( and Rust programs that define extern blocks )
    • If you want to expose any portion of the a cdylib's interface over the C ABI you must use Extern Functions.
    • If you want to link a Rust program to a Rust cdylib over the C ABI you have to use Extern Blocks ( See Rust reference doc ).
  • When building a cdylib, any functions that are not exposed through an extern block will be automatically stripped from the generated library as an optimization.
    • For example, if you build a cdylib for a large rust library and you do not export any functions using the extern keyword, the library will be essentially empty and will be about 2.2M ( because that is the minimum size for a cdylib, again, possibly because it statically links the standard library ).

Dylib

dylib's are Rust shared libraries which have an unstable ABI that can change across Rust releases.

  • To build a dylib, you probably need the -C prefer-dynamic rustflag set so that it will not attempt to statically link the standard library.
  • Within the same Rust version, you can dynamically link to a Rust dylib from a Rust crate without having to use extern functions or blocks because it will go over the Rust ABI.
  • dylib's will not strip out any unused functions when built. Even if none of the functions in the library are explicitly expose or used in the crate, all of the functions will still be included in the generated library.
30 Likes