@@ -4006,45 +4006,45 @@ compiler must at some point make a choice between these two formats. With this
4006
4006
in mind, the compiler follows these rules when determining what format of
4007
4007
dependencies will be used:
4008
4008
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
4021
4010
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 .
4023
4012
4024
4013
Note that it is impossible to link in native dynamic dependencies to a static
4025
4014
library, and in this case warnings will be printed about all unlinked native
4026
4015
dynamic dependencies.
4027
4016
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
4029
4018
format the upstream dependencies are available in. It is simply required that
4030
4019
all upstream dependencies be available for reading metadata from.
4031
4020
4032
4021
The reason for this is that ` rlib ` files do not contain any of their upstream
4033
4022
dependencies. It wouldn't be very efficient for all ` rlib ` files to contain a
4034
4023
copy of ` libstd.rlib ` !
4035
4024
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.
4048
4048
4049
4049
In general, ` --crate-type=bin ` or ` --crate-type=lib ` should be sufficient for
4050
4050
all compilation needs, and the other options are just available if more
0 commit comments