11# Linkage
22
3+ r[ link]
4+
35> Note: This section is described more in terms of the compiler than of
46> the language.
57
8+ r[ link.intro]
69The compiler supports various methods to link crates together both
710statically and dynamically. This section will explore the various methods to
811link crates together, and more information about native libraries can be
912found in the [ FFI section of the book] [ ffi ] .
1013
1114[ ffi ] : ../book/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code
1215
16+ r[ link.type]
1317In one session of compilation, the compiler can generate multiple artifacts
1418through the usage of either command line flags or the ` crate_type ` attribute.
1519If one or more command line flags are specified, all ` crate_type ` attributes will
1620be ignored in favor of only building the artifacts specified by command line.
1721
22+ r[ link.bin]
1823* ` --crate-type=bin ` , ` #![crate_type = "bin"] ` - A runnable executable will be
1924 produced. This requires that there is a ` main ` function in the crate which
2025 will be run when the program begins executing. This will link in all Rust and
2126 native dependencies, producing a single distributable binary.
2227 This is the default crate type.
2328
29+ r[ link.lib]
2430* ` --crate-type=lib ` , ` #![crate_type = "lib"] ` - A Rust library will be produced.
2531 This is an ambiguous concept as to what exactly is produced because a library
2632 can manifest itself in several forms. The purpose of this generic ` lib ` option
@@ -30,13 +36,15 @@ be ignored in favor of only building the artifacts specified by command line.
3036 libraries, and the ` lib ` type can be seen as an alias for one of them (but the
3137 actual one is compiler-defined).
3238
39+ r[ link.dylib]
3340* ` --crate-type=dylib ` , ` #![crate_type = "dylib"] ` - A dynamic Rust library will
3441 be produced. This is different from the ` lib ` output type in that this forces
3542 dynamic library generation. The resulting dynamic library can be used as a
3643 dependency for other libraries and/or executables. This output type will
3744 create ` *.so ` files on Linux, ` *.dylib ` files on macOS, and ` *.dll ` files on
3845 Windows.
3946
47+ r[ link.staticlib]
4048* ` --crate-type=staticlib ` , ` #![crate_type = "staticlib"] ` - A static system
4149 library will be produced. This is different from other library outputs in that
4250 the compiler will never attempt to link to ` staticlib ` outputs. The
@@ -62,12 +70,14 @@ be ignored in favor of only building the artifacts specified by command line.
6270 dependencies that is not actually used (e.g. ` --gc-sections ` or ` -dead_strip `
6371 for macOS).
6472
73+ r[ link.cdylib]
6574* ` --crate-type=cdylib ` , ` #![crate_type = "cdylib"] ` - A dynamic system
6675 library will be produced. This is used when compiling
6776 a dynamic library to be loaded from another language. This output type will
6877 create ` *.so ` files on Linux, ` *.dylib ` files on macOS, and ` *.dll ` files on
6978 Windows.
7079
80+ r[ link.rlib]
7181* ` --crate-type=rlib ` , ` #![crate_type = "rlib"] ` - A "Rust library" file will be
7282 produced. This is used as an intermediate artifact and can be thought of as a
7383 "static Rust library". These ` rlib ` files, unlike ` staticlib ` files, are
@@ -76,6 +86,7 @@ be ignored in favor of only building the artifacts specified by command line.
7686 in dynamic libraries. This form of output is used to produce statically linked
7787 executables as well as ` staticlib ` outputs.
7888
89+ r[ link.proc-macro]
7990* ` --crate-type=proc-macro ` , ` #![crate_type = "proc-macro"] ` - The output
8091 produced is not specified, but if a ` -L ` path is provided to it then the
8192 compiler will recognize the output artifacts as a macro and it can be loaded
@@ -87,13 +98,15 @@ be ignored in favor of only building the artifacts specified by command line.
8798 ` x86_64-unknown-linux-gnu ` even if the crate is a dependency of another crate
8899 being built for a different target.
89100
101+ r[ link.repetition]
90102Note that these outputs are stackable in the sense that if multiple are
91103specified, then the compiler will produce each form of output without
92104having to recompile. However, this only applies for outputs specified by the
93105same method. If only ` crate_type ` attributes are specified, then they will all
94106be built, but if one or more ` --crate-type ` command line flags are specified,
95107then only those outputs will be built.
96108
109+ r[ link.dependency]
97110With all these different kinds of outputs, if crate A depends on crate B, then
98111the compiler could find B in various different forms throughout the system. The
99112only forms looked for by the compiler, however, are the ` rlib ` format and the
@@ -102,6 +115,7 @@ compiler must at some point make a choice between these two formats. With this
102115in mind, the compiler follows these rules when determining what format of
103116dependencies will be used:
104117
118+ r[ link.dependency-staticlib]
1051191 . If a static library is being produced, all upstream dependencies are
106120 required to be available in ` rlib ` formats. This requirement stems from the
107121 reason that a dynamic library cannot be converted into a static format.
@@ -110,6 +124,8 @@ dependencies will be used:
110124 library, and in this case warnings will be printed about all unlinked native
111125 dynamic dependencies.
112126
127+ r[ link.dependency-rlib]
128+
1131292 . If an ` rlib ` file is being produced, then there are no restrictions on what
114130 format the upstream dependencies are available in. It is simply required that
115131 all upstream dependencies be available for reading metadata from.
@@ -118,11 +134,15 @@ dependencies will be used:
118134 dependencies. It wouldn't be very efficient for all ` rlib ` files to contain a
119135 copy of ` libstd.rlib ` !
120136
137+ r[ link.dependency-prefer-dynamic]
138+
1211393 . If an executable is being produced and the ` -C prefer-dynamic ` flag is not
122140 specified, then dependencies are first attempted to be found in the ` rlib `
123141 format. If some dependencies are not available in an rlib format, then
124142 dynamic linking is attempted (see below).
125143
144+ r[ link.dependency-dynamic]
145+
1261464 . If a dynamic library or an executable that is being dynamically linked is
127147 being produced, then the compiler will attempt to reconcile the available
128148 dependencies in either the rlib or dylib format to create a final product.
@@ -148,6 +168,9 @@ fine-grained control is desired over the output format of a crate.
148168
149169## Static and dynamic C runtimes
150170
171+ r[ link.crt]
172+
173+ r[ link.crt.intro]
151174The standard library in general strives to support both statically linked and
152175dynamically linked C runtimes for targets as appropriate. For example the
153176` x86_64-pc-windows-msvc ` and ` x86_64-unknown-linux-musl ` targets typically come
@@ -162,6 +185,7 @@ default such as:
162185* ` i686-unknown-linux-musl `
163186* ` x86_64-unknown-linux-musl `
164187
188+ r[ link.crt.crt-static]
165189The linkage of the C runtime is configured to respect the ` crt-static ` target
166190feature. These target features are typically configured from the command line
167191via flags to the compiler itself. For example to enable a static runtime you
@@ -177,10 +201,12 @@ whereas to link dynamically to the C runtime you would execute:
177201rustc -C target-feature=-crt-static foo.rs
178202```
179203
204+ r[ link.crt.ineffective]
180205Targets which do not support switching between linkage of the C runtime will
181206ignore this flag. It's recommended to inspect the resulting binary to ensure
182207that it's linked as you would expect after the compiler succeeds.
183208
209+ r[ link.crt.target_feature]
184210Crates may also learn about how the C runtime is being linked. Code on MSVC, for
185211example, needs to be compiled differently (e.g. with ` /MT ` or ` /MD ` ) depending
186212on the runtime being linked. This is exported currently through the
0 commit comments