tools | compiler (linkage | flags | crate-type)
Add a new crate type accepted by the compiler, called cdylib
, which
corresponds to exporting a C interface from a Rust dynamic library.
Currently the compiler supports two modes of generating dynamic libraries:
Each of these flavors of dynamic libraries has a distinct use case. For examples rdylibs are used by the compiler itself to implement plugins, and cdylibs are used whenever Rust needs to be dynamically loaded from another language or application.
Unfortunately the balance of features is tilted a little bit too much towards the smallest use case, rdylibs. In practice because Rust is statically linked by default and has an unstable ABI, rdylibs are used quite rarely. There are a number of requirements they impose, however, which aren't necessary for cdylibs:
The purpose of this RFC is to solve these drawbacks with a new crate-type to represent the more rarely used form of dynamic library (rdylibs).
A new crate type will be accepted by the compiler, cdylib
, which can be passed
as either --crate-type cdylib
on the command line or via #![crate_type = "cdylib"]
in crate attributes. This crate type will conceptually correspond to
the cdylib use case described above, and today's dylib
crate-type will
continue to correspond to the rdylib use case above. Note that the literal
output artifacts of these two crate types (files, file names, etc) will be the
same.
The two formats will differ in the parts listed in the motivation above, specifically:
pub fn foo() {}
will
not be an exported symbol, but #[no_mangle] pub extern fn foo() {}
will be
an exported symbol. Note that the compiler will also be at liberty to pass
extra flags to the linker to actively hide exported Rust symbols from linked
libraries.Rust's ephemeral and ill-defined "linkage model" is... well... ill defined and ephemeral. This RFC is an extension of this model, but it's difficult to reason about extending that which is not well defined. As a result there could be unforseen interactions between this output format and where it's used.
rdylib
, instead of
adding a new crate type, cdylib
. The existing dylib
output type would be
reinterpreted as a cdylib use-case. This is unfortunately, however, a breaking
change and requires a somewhat complicated transition plan in Cargo for
plugins. In the end it didn't seem worth it for the benefit of "cdylib is
probably what you want".dylib
format be considered unstable? (should it require
a nightly compiler?). The use case for a Rust dynamic library is so limited,
and so volatile, we may want to just gate access to it by default.