1
1
# Linkage
2
2
3
- The Rust compiler supports various methods to link crates together both
3
+ > Note: This section is described more in terms of the compiler than of
4
+ > the language.
5
+
6
+ The compiler supports various methods to link crates together both
4
7
statically and dynamically. This section will explore the various methods to
5
- link Rust crates together, and more information about native libraries can be
8
+ link crates together, and more information about native libraries can be
6
9
found in the [ FFI section of the book] [ ffi ] .
7
10
8
11
[ ffi ] : ../book/ffi.html
@@ -35,7 +38,7 @@ be ignored in favor of only building the artifacts specified by command line.
35
38
36
39
* ` --crate-type=staticlib ` , ` #[crate_type = "staticlib"] ` - A static system
37
40
library will be produced. This is different from other library outputs in that
38
- the Rust compiler will never attempt to link to ` staticlib ` outputs. The
41
+ the compiler will never attempt to link to ` staticlib ` outputs. The
39
42
purpose of this output type is to create a static library containing all of
40
43
the local crate's code along with all upstream dependencies. The static
41
44
library is actually a ` *.a ` archive on linux and osx and a ` *.lib ` file on
@@ -44,28 +47,29 @@ be ignored in favor of only building the artifacts specified by command line.
44
47
dynamic dependencies on other Rust code.
45
48
46
49
* ` --crate-type=cdylib ` , ` #[crate_type = "cdylib"] ` - A dynamic system
47
- library will be produced. This is used when compiling Rust code as
50
+ library will be produced. This is used when compiling
48
51
a dynamic library to be loaded from another language. This output type will
49
52
create ` *.so ` files on Linux, ` *.dylib ` files on macOS, and ` *.dll ` files on
50
53
Windows.
51
54
52
55
* ` --crate-type=rlib ` , ` #[crate_type = "rlib"] ` - A "Rust library" file will be
53
56
produced. This is used as an intermediate artifact and can be thought of as a
54
57
"static Rust library". These ` rlib ` files, unlike ` staticlib ` files, are
55
- interpreted by the Rust compiler in future linkage. This essentially means
58
+ interpreted by the compiler in future linkage. This essentially means
56
59
that ` rustc ` will look for metadata in ` rlib ` files like it looks for metadata
57
60
in dynamic libraries. This form of output is used to produce statically linked
58
61
executables as well as ` staticlib ` outputs.
59
62
60
63
* ` --crate-type=proc-macro ` , ` #[crate_type = "proc-macro"] ` - The output
61
64
produced is not specified, but if a ` -L ` path is provided to it then the
62
65
compiler will recognize the output artifacts as a macro and it can be loaded
63
- for a program. If a crate is compiled with the ` proc-macro ` crate type it
64
- will forbid exporting any items in the crate other than those functions
65
- tagged ` #[proc_macro_derive] ` and those functions must also be placed at the
66
- crate root. Finally, the compiler will automatically set the
67
- ` cfg(proc_macro) ` annotation whenever any crate type of a compilation is the
68
- ` proc-macro ` crate type.
66
+ for a program. Crates compiled with this crate type must only export
67
+ [ procedural macros] . The compiler will automatically set the ` proc_macro `
68
+ [ configuration option] . The crates are always compiled with the same target
69
+ that the compiler itself was built with. For example, if you are executing
70
+ the compiler from Linux with an ` x86_64 ` CPU, the target will be
71
+ ` x86_64-unknown-linux-gnu ` even if the crate is a dependency of another crate
72
+ being built for a different target.
69
73
70
74
Note that these outputs are stackable in the sense that if multiple are
71
75
specified, then the compiler will produce each form of output at once without
@@ -124,7 +128,7 @@ dependencies will be used:
124
128
125
129
In general, ` --crate-type=bin ` or ` --crate-type=lib ` should be sufficient for
126
130
all compilation needs, and the other options are just available if more
127
- fine-grained control is desired over the output format of a Rust crate.
131
+ fine-grained control is desired over the output format of a crate.
128
132
129
133
## Static and dynamic C runtimes
130
134
@@ -205,3 +209,6 @@ a statically linked binary on MSVC you would execute:
205
209
``` ignore,notrust
206
210
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
207
211
```
212
+
213
+ [ configuration option ] : conditional-compilation.html
214
+ [ procedural macros ] : procedural-macros.html
0 commit comments