Skip to content

Libraries exported by cc_shared_library can be dropped by the linker #20428

@cameron-martin

Description

@cameron-martin

Description of the bug:

If a library (without alwayslink = True) is exported by a cc_shared_library but unused, the linker drops the library. This is problematic since if a shared library (A) depends on a shared library (B), it expects any of its dependencies that are exported by B to be fulfilled by B, and therefore does not link them into A. The only way of avoiding this is to specify this dropped library in deps.

I would expect libraries that match the exports filter to be treated as if they were alwayslink, i.e. not put them inside -Wl,--start-lib and -Wl,--end-lib.

There is a slack thread about this here: https://bazelbuild.slack.com/archives/CGA9QFQ8H/p1701702759075819

Which category does this issue belong to?

No response

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

I have created a reproducer of this issue here: https://github.com/cameron-martin/bazel-7-cc-shared-library-dropping-exported

Running bazel build //:bin reproduces this error, getting a linker error due to the dropped library.

Which operating system are you running Bazel on?

Ubuntu 20.04

What is the output of bazel info release?

release 7.0.0rc5

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3We're not considering working on this, but happy to review a PR. (No assignee)help wantedSomeone outside the Bazel team could own thisteam-Rules-CPPIssues for C++ rulestype: feature request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions