Skip to content

+crt-static is not respected on most of the targets #1711

Closed
@petrochenkov

Description

@petrochenkov

If I want to link statically to e.g. Android libc, the libc crate will ignore the +crt-static request and will link libc dynamically anyway.

The libc crate should respect user requests like this and link to libc statically in #[cfg(target_feature = "crt-static")] mode if the target provides a static version of libc at all.

The relevant code is here:

libc/src/unix/mod.rs

Lines 295 to 357 in bcbfeb5

cfg_if! {
if #[cfg(target_os = "l4re")] {
// required libraries for L4Re are linked externally, ATM
} else if #[cfg(feature = "std")] {
// cargo build, don't pull in anything extra as the libstd dep
// already pulls in all libs.
} else if #[cfg(target_env = "musl")] {
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", kind = "static",
cfg(target_feature = "crt-static")))]
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static"))))]
extern {}
} else if #[cfg(target_os = "emscripten")] {
#[link(name = "c")]
extern {}
} else if #[cfg(all(target_os = "netbsd",
feature = "rustc-dep-of-std",
target_vendor = "rumprun"))] {
// Since we don't use -nodefaultlibs on Rumprun, libc is always pulled
// in automatically by the linker. We avoid passing it explicitly, as it
// causes some versions of binutils to crash with an assertion failure.
#[link(name = "m")]
extern {}
} else if #[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "openbsd"))] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
} else if #[cfg(target_os = "haiku")] {
#[link(name = "root")]
#[link(name = "network")]
extern {}
} else if #[cfg(target_env = "newlib")] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
} else if #[cfg(target_os = "hermit")] {
// no_default_libraries is set to false for HermitCore, so only a link
// to "pthread" needs to be added.
#[link(name = "pthread")]
extern {}
} else if #[cfg(target_env = "illumos")] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
} else if #[cfg(target_os = "redox")] {
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", kind = "static-nobundle",
cfg(target_feature = "crt-static")))]
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static"))))]
extern {}
} else {
#[link(name = "c")]
#[link(name = "m")]
#[link(name = "rt")]
#[link(name = "pthread")]
extern {}
}
}

Static libc may require additionally linking some startup objects etc, and for some targets this is supported implicitly by rustc, but such support is not critical because the necessary linking can be done explicitly by the user with -C link-args and similar options.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions