Mislinking when using bazel with clang and libc++

1,043 views
Skip to first unread message

Thomas Köppe

unread,
Jan 7, 2021, 10:15:52 AM1/7/21
to bazel-...@googlegroups.com
I am trying to use the clang C++ compiler with Bazel, as well as the libc++ standard library implementation. To this end I set my system up as follows:
  • Install clang, libc++-dev, libc++abi-dev
  • Symlink /usr/lib/llvm-11/include/c++/v1 to /usr/include/c++/v1
  • Cet the environment CC=clang-11
At this point, note that regular building works as expected and uses clang, but still with libstdc++.

Now I switch to libc++ by using the following build flags:
  • --copt=-stdlib=libc++ --linkopt=-lc++abi --linkopt=-lc++
What I now see is that the resulting binary is linked _both_ against libc++ and libstdc++. I made a tiny toy cc_library :foo, and here are the relevant entries in bazel-bin/foo-2.params:

-o
bazel-out/k8-opt/bin/foo
-fuse-ld=/usr/bin/ld.gold
-Wl,-no-as-needed
-Wl,-z,relro,-z,now
-B/usr/bin
-lstdc++
-lm
-Wl,--gc-sections
bazel-out/k8-opt/bin/_objs/foo/foo.o
-lc++abi
-lc++

Is there anything I'm doing wrong, and how should I go about using libc++?

Thank you!

Thomas

re:fi.64

unread,
Jan 7, 2021, 11:06:36 AM1/7/21
to Thomas Köppe, bazel-discuss
Check out Envoy's libc++ config, important parts start here: https://github.com/envoyproxy/envoy/blob/master/.bazelrc#L113

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CALkRRgfHQPyaXMkbBe%2B5sz6qB9sy8pL%3DX0ak6JRYRx5tJ2QaoQ%40mail.gmail.com.

tko...@google.com

unread,
Jan 7, 2021, 11:20:07 AM1/7/21
to bazel-discuss
Thank you very much, that works like a charm!

For the record, the solution is to use the following flags instead:

--action_env=CXXFLAGS=-stdlib=libc++ --action_env=LDFLAGS=-stdlib=libc++ --action_env=BAZEL_CXXOPTS=-stdlib=libc++ --action_env=BAZEL_LINKOPTS=-lc++:-lm

Reply all
Reply to author
Forward
0 new messages