RFC 1510: cdylib crate type

tools | compiler (linkage | flags | crate-type)

Summary

Add a new crate type accepted by the compiler, called cdylib, which corresponds to exporting a C interface from a Rust dynamic library.

Motivation

Currently the compiler supports two modes of generating dynamic libraries:

  1. One form of dynamic library is intended for reuse with further compilations. This kind of library exposes all Rust symbols, links to the standard library dynamically, etc. I'll refer to this mode as rdylib as it's a Rust dynamic library talking to Rust.
  2. Another form of dynamic library is intended for embedding a Rust application into another. Currently the only difference from the previous kind of dynamic library is that it favors linking statically to other Rust libraries (bundling them inside). I'll refer to this as a cdylib as it's a Rust dynamic library exporting a C API.

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).

Detailed design

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:

Drawbacks

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.

Alternatives

Unresolved questions