Skip to content

Commit e798b97

Browse files
committed
doc: Update the linkage documentation
After allowing mixing rlibs and dylibs in rust-lang#13892, the documentation was not updated accordingly to reflect this new capability.
1 parent 06ea989 commit e798b97

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/doc/rust.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4006,45 +4006,45 @@ compiler must at some point make a choice between these two formats. With this
40064006
in mind, the compiler follows these rules when determining what format of
40074007
dependencies will be used:
40084008

4009-
1. If a dynamic library is being produced, then it is required for all upstream
4010-
Rust dependencies to also be dynamic. This is a limitation of the current
4011-
implementation of the linkage model. The reason behind this limitation is to
4012-
prevent multiple copies of the same upstream library from showing up, and in
4013-
the future it is planned to support a mixture of dynamic and static linking.
4014-
4015-
When producing a dynamic library, the compiler will generate an error if an
4016-
upstream dependency could not be found, and also if an upstream dependency
4017-
could only be found in an `rlib` format. Remember that `staticlib` formats
4018-
are always ignored by `rustc` for crate-linking purposes.
4019-
4020-
2. If a static library is being produced, all upstream dependencies are
4009+
1. If a static library is being produced, all upstream dependencies are
40214010
required to be available in `rlib` formats. This requirement stems from the
4022-
same reasons that a dynamic library must have all dynamic dependencies.
4011+
reason that a dynamic library cannot be converted into a static format.
40234012

40244013
Note that it is impossible to link in native dynamic dependencies to a static
40254014
library, and in this case warnings will be printed about all unlinked native
40264015
dynamic dependencies.
40274016

4028-
3. If an `rlib` file is being produced, then there are no restrictions on what
4017+
2. If an `rlib` file is being produced, then there are no restrictions on what
40294018
format the upstream dependencies are available in. It is simply required that
40304019
all upstream dependencies be available for reading metadata from.
40314020

40324021
The reason for this is that `rlib` files do not contain any of their upstream
40334022
dependencies. It wouldn't be very efficient for all `rlib` files to contain a
40344023
copy of `libstd.rlib`!
40354024

4036-
4. If an executable is being produced, then things get a little interesting. As
4037-
with the above limitations in dynamic and static libraries, it is required
4038-
for all upstream dependencies to be in the same format. The next question is
4039-
whether to prefer a dynamic or a static format. The compiler currently favors
4040-
static linking over dynamic linking, but this can be inverted with the `-C
4041-
prefer-dynamic` flag to the compiler.
4042-
4043-
What this means is that first the compiler will attempt to find all upstream
4044-
dependencies as `rlib` files, and if successful, it will create a statically
4045-
linked executable. If an upstream dependency is missing as an `rlib` file,
4046-
then the compiler will force all dependencies to be dynamic and will generate
4047-
errors if dynamic versions could not be found.
4025+
3. If an executable is being produced and the `-C prefer-dynamic` flag is not
4026+
specified, then dependencies are first attempted to be found in the `rlib`
4027+
format. If some dependencies are not available in an rlib format, then
4028+
dynamic linking is attempted (see below).
4029+
4030+
4. If a dynamic library or an executable that is being dynamically linked is
4031+
being produced, then the compiler will attempt to reconcile the available
4032+
dependencies in either the rlib or dylib format to create a final product.
4033+
4034+
A major goal of the compiler is to ensure that a library never appears more
4035+
than once in any artifact. For example, if dynamic libraries B and C were
4036+
each statically linked to library A, then a crate could not link to B and C
4037+
together because there would be two copies of A. The compiler allows mixing
4038+
the rlib and dylib formats, but this restriction must be satisfied.
4039+
4040+
The compiler currently implements no method of hinting what format a library
4041+
should be linked with. When dynamically linking, the compiler will attempt to
4042+
maximize dynamic dependencies while still allowing some dependencies to be
4043+
linked in via an rlib.
4044+
4045+
For most situations, having all libraries available as a dylib is recommended
4046+
if dynamically linking. For other situations, the compiler will emit a
4047+
warning if it is unable to determine which formats to link each library with.
40484048

40494049
In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for
40504050
all compilation needs, and the other options are just available if more

0 commit comments

Comments
 (0)