Skip to content

1.0: Remove all instances of link(name = ...) #5265

Description

@tgross35

Currently, libc expects std to provide all link directives necessary. If the std feature is disabled, instead libc emits link attributes:

libc/src/unix/mod.rs

Lines 439 to 624 in f24764d

// FIXME(1.0): We want to remove these directives and instead expect that no-std users add their
// own link configuration when required, rather than unconditionally linking everything that may
// possibly be needed.
cfg_if! {
if #[cfg(any(
target_os = "l4re",
target_os = "espidf",
target_os = "nuttx"
))] {
// required libraries are linked externally for these platforms:
// * L4Re
// * ESP-IDF
// * NuttX
} else if #[cfg(feature = "std")] {
// cargo build, don't pull in anything extra as the std dep
// already pulls in all libs.
} else if #[cfg(all(
any(
all(
target_os = "linux",
any(target_env = "gnu", target_env = "uclibc")
),
target_os = "cygwin"
),
feature = "rustc-dep-of-std"
))] {
#[link(
name = "util",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "rt",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "pthread",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "m",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "dl",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "c",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "gcc_eh",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "gcc",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "c",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(name = "util", cfg(not(target_feature = "crt-static")))]
#[link(name = "rt", cfg(not(target_feature = "crt-static")))]
#[link(name = "pthread", cfg(not(target_feature = "crt-static")))]
#[link(name = "m", cfg(not(target_feature = "crt-static")))]
#[link(name = "dl", cfg(not(target_feature = "crt-static")))]
#[link(name = "c", cfg(not(target_feature = "crt-static")))]
extern "C" {}
} else if #[cfg(libc_pauthtest)] {
#[link(name = "c")]
#[link(name = "m")]
#[link(name = "rt")]
#[link(name = "pthread")]
#[link(name = "dl")]
extern "C" {}
} else if #[cfg(any(
all(target_env = "musl", not(libc_pauthtest)),
target_env = "ohos"
))] {
#[cfg_attr(
feature = "rustc-dep-of-std",
link(
name = "c",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)
)]
#[cfg_attr(
feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static")))
)]
extern "C" {}
} else if #[cfg(target_os = "emscripten")] {
// Don't pass -lc to Emscripten, it breaks. See:
// https://github.com/emscripten-core/emscripten/issues/22758
} else if #[cfg(all(target_os = "android", feature = "rustc-dep-of-std"))] {
#[link(
name = "c",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(
name = "m",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)]
#[link(name = "m", cfg(not(target_feature = "crt-static")))]
#[link(name = "c", cfg(not(target_feature = "crt-static")))]
extern "C" {}
} else if #[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
target_os = "android",
target_os = "openbsd",
target_os = "nto",
target_os = "qnx",
))] {
#[link(name = "c")]
#[link(name = "m")]
extern "C" {}
} else if #[cfg(target_os = "haiku")] {
#[link(name = "root")]
#[link(name = "network")]
extern "C" {}
} else if #[cfg(target_env = "newlib")] {
#[link(name = "c")]
#[link(name = "m")]
extern "C" {}
} else if #[cfg(target_env = "illumos")] {
#[link(name = "c")]
#[link(name = "m")]
extern "C" {}
} else if #[cfg(target_os = "redox")] {
#[cfg_attr(
feature = "rustc-dep-of-std",
link(
name = "c",
kind = "static",
modifiers = "-bundle",
cfg(target_feature = "crt-static")
)
)]
#[cfg_attr(
feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static")))
)]
extern "C" {}
} else if #[cfg(target_os = "aix")] {
#[link(name = "c")]
#[link(name = "m")]
#[link(name = "bsd")]
#[link(name = "pthread")]
extern "C" {}
} else {
#[link(name = "c")]
#[link(name = "m")]
#[link(name = "rt")]
#[link(name = "pthread")]
extern "C" {}
}
}

We discussed at rustweek 2025 that we should remove this and expect users to add their own link attributes when building in no-std environments. This way you can still use the bindings even if some of the libraries aren't available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions