diff --git a/src/attributes.md b/src/attributes.md index c66a8d2e1..389dd9c67 100644 --- a/src/attributes.md +++ b/src/attributes.md @@ -343,7 +343,7 @@ The following is an index of all built-in attributes. [`cold`]: attributes/codegen.md#the-cold-attribute [`collapse_debuginfo`]: attributes/debugger.md#the-collapse_debuginfo-attribute [`crate_name`]: crates-and-source-files.md#the-crate_name-attribute -[`crate_type`]: linkage.md +[`crate_type`]: linkage.md#the-crate_type-attribute [`debugger_visualizer`]: attributes/debugger.md#the-debugger_visualizer-attribute [`deny`]: attributes/diagnostics.md#lint-check-attributes [`deprecated`]: attributes/diagnostics.md#the-deprecated-attribute diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 7440fe0c2..23aa21c11 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -459,7 +459,7 @@ println!("I'm running on a {} machine!", machine_kind); [`cfg` macro]: #the-cfg-macro [`cfg_attr`]: #the-cfg_attr-attribute [`crate_name`]: crates-and-source-files.md#the-crate_name-attribute -[`crate_type`]: linkage.md +[`crate_type`]: linkage.md#the-crate_type-attribute [`target_feature` attribute]: attributes/codegen.md#the-target_feature-attribute [attribute]: attributes.md [attributes]: attributes.md diff --git a/src/linkage.md b/src/linkage.md index 5ce8f6fd5..ec862a56b 100644 --- a/src/linkage.md +++ b/src/linkage.md @@ -14,7 +14,7 @@ found in the [FFI section of the book][ffi]. r[link.type] In one session of compilation, the compiler can generate multiple artifacts -through the usage of either command line flags or the `crate_type` attribute. +through the usage of either command line flags or the [`crate_type` attribute]. If one or more command line flags are specified, all `crate_type` attributes will be ignored in favor of only building the artifacts specified by command line. @@ -105,6 +105,10 @@ same method. If only `crate_type` attributes are specified, then they will all be built, but if one or more `--crate-type` command line flags are specified, then only those outputs will be built. + + r[link.dependency] With all these different kinds of outputs, if crate A depends on crate B, then the compiler could find B in various different forms throughout the system. The @@ -165,6 +169,26 @@ In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for all compilation needs, and the other options are just available if more fine-grained control is desired over the output format of a crate. +r[link.crate_type] +## The `crate_type` attribute + +r[link.crate_type.intro] +The *`crate_type` [attribute][attributes]* is used to specify the [crate type][link.type] of the crate. + +> [!EXAMPLE] +> ```rust +> #![crate_type = "lib"] +> ``` + +r[link.crate_type.syntax] +The `crate_type` attribute uses the [MetaNameValueStr] syntax to specify the crate type. + +r[link.crate_type.allowed-positions] +The `crate_type` attribute may only be applied to the crate root. + +r[link.crate_type.duplicates] +If the `crate_type` is specified multiple times, then the crate is built for all of the given crate types. + r[link.crt] ## Static and dynamic C runtimes @@ -302,3 +326,4 @@ If a Rust artifact is potentially unwinding, then all its crates must be built w [procedural macros]: procedural-macros.md [panic strategy]: panic.md#panic-strategy [`-C panic`]: ../rustc/codegen-options/index.html#panic +[`crate_type` attribute]: link.crate_type