Create a fully static C++ binary in Bazel

Viewed 580

Is it possible to create a fully static binary in Bazel or is there a bug in GCC or bazel preventing me.

This is the error that I get

/bin/ld.gold: error: /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o: requires dynamic R_X86_64_32 reloc against '__TMC_END__' which may overflow at runtime; recompile with -fPIC

Which if I understand correctly is a version libstdc++ that is not statically linked. Was wondering if there was a version of libstdc++ that is statically linked that I could make Bazel use.

Tried looking at this issue on Bazel's repo but the workarounds mentioned do not work for me

https://github.com/bazelbuild/bazel/issues/8672

Info Dump

  • Bazel version:3.3.1
  • GCC version: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
  • OS: Ubuntu 20.04 in a Docker container

My build file

cc_binary(
  name = "main",
  srcs = ["main.cpp"],
  linkshared=True,
  copts = ["-shared", "-fpic", "-fPIC"],
  features = ["fully_static_link"],
)

main.cpp

int main()
{
  return 1;
}

Other versions of the build file with similar results

cc_binary(
    name = "main",
    srcs = ["main.cpp"],
    linkshared=True,
    linkstatic=1,
    linkopts = ["-static"]
)

cc_binary(
    name = "main",
    srcs = ["main.cpp"],
    linkshared=True,
    linkstatic=1,
    linkopts = ["-static"],
    features = ["fully_static_link"],
    copts=["-fpic"],
)

TL;DR

Bazel will not compile a fully static binary in Ubuntu 20.04, GCC 9.3.0. I'm seeking to understand if the issue posted is related or if I don't understand something about static linking?

0 Answers
Related