How to link libstdc++ statically with Bazel?

3,104 views
Skip to first unread message

kirank...@gmail.com

unread,
Aug 29, 2018, 9:01:31 AM8/29/18
to bazel-discuss
We are really struggling to create an executable that is statically linked with libstdc++. We looked at the strace and found that bazel is by default adding -lstdc++ list of linker flags. Because of this even though we are providing -l:libstdc++.a linker flag, executable is still getting linked dynamically with libstdc++. Can someone please help us in getting this working.


ubuntu@ubuntu$ ldd instance_service_main
linux-vdso.so.1 => (0x00007fff6a6fe000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4afb88d000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f4afb685000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4afb481000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4afb0ff000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4afadf6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4afaa2c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4afbaaa000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4afa816000)

Thanks :)

Please note that we are already passing the below mentioned arguments to the linker options in cc_binary but in vain. We need someway to suppress the -lstdc++ flag and link statically with libstdc++.a through bazel.

"-static-libgcc",
"-static-libstdc++",
"-Wl,-l:libgcc.a",
"-Wl,-l:libstdc++.a",
"-Wl,/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a",
"-Wl,-l/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a",

Marcel Hlopko

unread,
Dec 28, 2018, 2:01:13 AM12/28/18
to kirank...@gmail.com, bazel-discuss
Hi Sai Kiran,

did you manage to link libc++ statically? I added some docs to cc_toolchain (will appear on the bazel.build with 0.22 release): https://source.bazel.build/bazel/+/331796f30f5f2804e2fe293ca9d5603acb029a6c:src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainRule.java;l=247 and also this flag is relevant: https://github.com/bazelbuild/bazel/issues/6942

Let me know if you need more info.

Thanks!

--
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/14ef4520-53f5-43f6-976e-067fd493cbba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Geschäftsführer: Paul Manicle, Halimah DeLaine Prado | Registergericht und -nummer: Hamburg, HRB 86891

Sai Kiran Katuri

unread,
Dec 28, 2018, 1:58:26 PM12/28/18
to Marcel Hlopko, bazel-discuss
Hey Marcel,
            Thanks a lot for your email. Yes, we are able to link the libstdc++ and libgcc statically(with libc still being linked dynamically, this was the original goal). We had to use a custom tool chain to achieve this. This tool chain uses normal gcc compiler with our specific arguments and removes some arguments(like -lstdc++) from the default tool chain. We therefore used CC_TOOLCHAIN and CC_TOOLCHAIN_SUITE and built this specific binary with this tool chain suite to achieve this task.

Thanks,
Sai Kiran Katuri.

coert...@gmail.com

unread,
Feb 13, 2019, 3:47:47 AM2/13/19
to bazel-discuss
Hi Sai Kiran Katuri,

I am trying to link libstdc++ statically to tensorflow using bazel. Would it be possible to share some details on how you got this to work? That would be very helpful.

Thanks!
Coert

Op vrijdag 28 december 2018 19:58:26 UTC+1 schreef Sai Kiran Katuri:

Sai Kiran Katuri

unread,
Mar 18, 2019, 11:43:10 AM3/18/19
to coert...@gmail.com, bazel-discuss
Hey Coert Metz,
              Sincerely apologies for a very late reply 😖. I kept your email on hold but kind of lost it as this is my personal email account. So, in summary, none of the bazel in build flags actually worked for us because bazel was adding the -lg++ flag automatically in its tool chain. Therefore, the solution that worked for us is to write our own custom cross tool chain by omitting the dynamic flags. After writing the custom cross tool, we changed our build script to make use of this custom cross tool for the binaries that are needed to be built statically.

Command: bazel build --crosstool_top=//<path-to-crosstool>/CROSSTOOL --dynamic_mode=default //<path-to-bazel-target>:bazel-target-name

Apologies for the late reply again. Please let me know if you have any more questions. Also, if you have found any other better solution that this, please let me know ☺

Thanks,
Sai Kiran Katuri.

You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/WM2OzIejonc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/958cb068-b1e5-485b-a21b-3394c2cfb9b4%40googlegroups.com.

lin tanghui

unread,
Jul 4, 2023, 11:23:57 PM7/4/23
to bazel-discuss
I had slove this by just add this two opt in 。bazelrc
```
build --action_env=BAZEL_LINKLIBS='-l%:libstdc++.a'
build --action_env=BAZEL_LINKOPTS='-static-libstdc++ -lm'
```
this env vars will replace bazel default linkopt
Reply all
Reply to author
Forward
0 new messages