11# Linkage
22
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
47statically 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
69found in the [ FFI section of the book] [ ffi ] .
710
811[ ffi ] : ../book/ffi.html
@@ -35,7 +38,7 @@ be ignored in favor of only building the artifacts specified by command line.
3538
3639* ` --crate-type=staticlib ` , ` #[crate_type = "staticlib"] ` - A static system
3740 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
3942 purpose of this output type is to create a static library containing all of
4043 the local crate's code along with all upstream dependencies. The static
4144 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.
4447 dynamic dependencies on other Rust code.
4548
4649* ` --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
4851 a dynamic library to be loaded from another language. This output type will
4952 create ` *.so ` files on Linux, ` *.dylib ` files on macOS, and ` *.dll ` files on
5053 Windows.
5154
5255* ` --crate-type=rlib ` , ` #[crate_type = "rlib"] ` - A "Rust library" file will be
5356 produced. This is used as an intermediate artifact and can be thought of as a
5457 "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
5659 that ` rustc ` will look for metadata in ` rlib ` files like it looks for metadata
5760 in dynamic libraries. This form of output is used to produce statically linked
5861 executables as well as ` staticlib ` outputs.
5962
6063* ` --crate-type=proc-macro ` , ` #[crate_type = "proc-macro"] ` - The output
6164 produced is not specified, but if a ` -L ` path is provided to it then the
6265 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.
6973
7074Note that these outputs are stackable in the sense that if multiple are
7175specified, then the compiler will produce each form of output at once without
@@ -124,7 +128,7 @@ dependencies will be used:
124128
125129In general, ` --crate-type=bin ` or ` --crate-type=lib ` should be sufficient for
126130all 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.
128132
129133## Static and dynamic C runtimes
130134
@@ -205,3 +209,6 @@ a statically linked binary on MSVC you would execute:
205209``` ignore,notrust
206210RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
207211```
212+
213+ [ configuration option ] : conditional-compilation.html
214+ [ procedural macros ] : procedural-macros.html
0 commit comments