bazel: Transitive `cc_shared_library` doesn't work with inline functions

Description of the bug:

cc_shared_library and dynamic_deps deliberately link the shared libraries for only direct dependencies, while excluding the corresponding static libraries transitively. If any of the direct dependencies have inline functions which use transitive dependencies, this can result in linking failures. This is common with C++ code, it’s less common but I think it’s still possible with C.

Having to specify all the transitive dependencies in dynamic_deps is not good. It’s even worse because sometimes with some compilers and some optimization settings it’s necessary, and other times it’s not (it depends on what gets inlined where).

Linking all of the transitive shared libraries seems low-cost. They’re going to be needed at runtime anyways. Would that be an acceptable change?

Which category does this issue belong to?

C++ Rules

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

https://github.com/brians-neptune/bazel-cc_shared_library-transitive-deps has a main_dynamic target which fails to link, but I think it should. main_static is the same thing without dynamic_deps, which does link.

Which operating system are you running Bazel on?

Ubuntu 22.04

What is the output of bazel info release?

release 7.0.2

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 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

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (9 by maintainers)

Most upvoted comments

It seems to me that implementation_deps is the solution to this issue. The headers from dependencies listed via deps are accessible to the dependents of that target, so that means the symbols are also accessible and must be available to the linker.

I think the proper solution when linking a binary with dynamic_deps is to traverse the dependency tree and link in all of the transitive deps. If a target has private dependencies that should not be available to its dependents, then those dependencies should be added to implementation_deps which will prevent them from being linked up the dependency tree.

I agree with Thomas. One additional note to add: you need to traverse all the transitive dynamic_deps when linking each intermediate shared library too, unless you link with -z undefs.

ERROR: /home/brian/Desktop/bazel-cc_shared_library-transitive-deps/BUILD:24:10: Linking main_dynamic failed: (Exit 1): cc_wrapper.sh failed: error executing CppLink command (from target //:main_dynamic) external/llvm_toolchain/bin/cc_wrapper.sh @bazel-out/k8-fastbuild/bin/main_dynamic-2.params

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
ld.lld: error: undefined symbol: transitive_return42()
>>> referenced by main.cc
>>>               bazel-out/k8-fastbuild/bin/_objs/main_dynamic/main.pic.o:(dep_return42())
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Use --verbose_failures to see the command lines of failed build steps.