bazel: fully_static_link is broken

Description of the problem / feature request:

fully_static_link is broken.

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

WORKSPACE

BUILD

cc_binary(
    name = "a",
    srcs = ["a.cc"],
)

a.cc

int main() { new int; }

Build command and output:

% bazel build --features=fully_static_link :a
INFO: Analyzed target //:a (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /usr/local/google/home/iceboy/tmp/hellobazel/BUILD:1:1: Linking of rule '//:a' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc @bazel-out/k8-fastbuild/bin/a-2.params

Use --sandbox_debug to see verbose messages from the sandbox
bazel-out/k8-fastbuild/bin/_objs/a/a.pic.o:a.cc:function main: error: undefined reference to 'operator new(unsigned long)'
collect2: error: ld returned 1 exit status
Target //:a failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.190s, Critical Path: 0.10s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

What operating system are you running Bazel on?

Linux

What’s the output of bazel info release?

release 0.27.0

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

Could you also share the state-of-the-art way of doing mostly static build? That is, linking libc and libm dynamically while linking other libraries (including libstdc++) statically?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

For who has the same problem, I put the following flags in .bazelrc to statically build binaries:

build --action_env=BAZEL_LINKLIBS=-l%:libstdc++.a:-lm
build --action_env=BAZEL_LINKOPTS=-static-libgcc

This will statically link libc++ and libgcc, dynamically link libc and libm, and be ABI compatible with most linux distros.

It seems odd that this is a P3 when there is a significant number of projects affected by this, including Envoy (they have a local workaround).

I was able to build statically with this:

    linkopts = ["-l:libstdc++.a"],
    features = ["fully_static_link"],

It should be straightforward to fix cc_configure to add -l:libstdc++.a to the end of the link command, right?