Skip to content

Commit 0f7130e

Browse files
committed
Add identifier syntax to linkage.md
1 parent 687faf9 commit 0f7130e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/linkage.md

+26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
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]
69
The compiler supports various methods to link crates together both
710
statically and dynamically. This section will explore the various methods to
811
link crates together, and more information about native libraries can be
912
found 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]
1317
In one session of compilation, the compiler can generate multiple artifacts
1418
through the usage of either command line flags or the `crate_type` attribute.
1519
If one or more command line flags are specified, all `crate_type` attributes will
1620
be 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]
90102
Note that these outputs are stackable in the sense that if multiple are
91103
specified, then the compiler will produce each form of output without
92104
having to recompile. However, this only applies for outputs specified by the
93105
same method. If only `crate_type` attributes are specified, then they will all
94106
be built, but if one or more `--crate-type` command line flags are specified,
95107
then only those outputs will be built.
96108

109+
r[link.dependency]
97110
With all these different kinds of outputs, if crate A depends on crate B, then
98111
the compiler could find B in various different forms throughout the system. The
99112
only 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
102115
in mind, the compiler follows these rules when determining what format of
103116
dependencies will be used:
104117

118+
r[link.dependency-staticlib]
105119
1. 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+
113129
2. 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+
121139
3. 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+
126146
4. 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]
151174
The standard library in general strives to support both statically linked and
152175
dynamically 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]
165189
The linkage of the C runtime is configured to respect the `crt-static` target
166190
feature. These target features are typically configured from the command line
167191
via 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:
177201
rustc -C target-feature=-crt-static foo.rs
178202
```
179203

204+
r[link.crt.ineffective]
180205
Targets which do not support switching between linkage of the C runtime will
181206
ignore this flag. It's recommended to inspect the resulting binary to ensure
182207
that it's linked as you would expect after the compiler succeeds.
183208

209+
r[link.crt.target_feature]
184210
Crates may also learn about how the C runtime is being linked. Code on MSVC, for
185211
example, needs to be compiled differently (e.g. with `/MT` or `/MD`) depending
186212
on the runtime being linked. This is exported currently through the

0 commit comments

Comments
 (0)