-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
static_link_cpp_runtimes doesn't link the libstdc++ statically #14342
Comments
I just looked briefly but it looks to me like this just isn't supported in bazel, maybe it is in blaze
|
Thanks. I think it's only the auto-configuration that's not enabled as it seems you can define the static_link_cpp_runtimes_feature = feature(
name = "static_link_cpp_runtimes",
enabled = False,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.lto_index_for_executable,
ACTION_NAMES.lto_index_for_dynamic_library,
],
flag_groups = [flag_group(flags = ["-static-libstdc++"])],
),
],
) (and also add this to the list of features that are exported), then I can get static linking to work by setting flag_set(
actions = all_link_actions + lto_index_actions,
flag_groups = [flag_group(flags = ["-lstdc++"])],
with_features = [
with_feature_set(
not_features = ["static_link_cpp_runtimes"],
),
],
), and then I modified the default value of I wonder if it's worth patching bazel's
This would make it much easier to statically or dynamically link the c++ standard library than it is now, and would enable users to enable it either per binary, or globally. I'd be happy to work on a patch if this sounds like a useful direction. |
I would find this very useful. Making the auto-configured toolchains work well with all advertised Bazel features is also especially helpful for those new to Bazel. |
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team ( |
This is an ongoing issue and should not be marked as stale. The workaround of setting BAZEL_LINKLIBS works for me on Linux (e.g. |
See https://bazel.build/docs/cc-toolchain-config-reference#wellknown-features for a description of the feature. See bazelbuild/bazel#14342 (comment) for the patch. We prefer patching to forking to benefit from upstream improvements to the toolchain config. CMK-18270 Change-Id: I199b4b22ed9c5aa32f6065400044dc57ad5fabcb
I've added the feature to
That's basically all there is to it. Change is Checkmk/checkmk@3ee513a |
@Synss Would you be willing to send a PR to Bazel with this change? It looks good to me and I can also help find a reviewer. |
Of course! I’ll be happy if the patch gets upstreamed and I don’t have to maintain it! 😉 I’ll look into sending a PR tomorrow. |
This patch adds the `static_link_cpp_runtimes` feature to `unix_cc_toolchain_config`. The feature is documented as a well-known feature[1] but not implemented. The patch implements the solution proposed on GitHub.[2] For the feature to work as expected, users have to remove the linker flag `"-lstdc++"` from `link_libs` (or `link_flags`) from the call to `unix_cc_toolchain_config`. [1] https://bazel.build/docs/cc-toolchain-config-reference#wellknown-features [2] bazelbuild#14342
This patch implements the `static_link_cpp_runtimes` feature in `unix_cc_toolchain_config`. The feature is documented as a well-known feature[1] and was added to the toolchain[2] but not implemented. The patch follows the solution proposed on GitHub.[3] For the feature to work as expected, users have to remove the linker flag `"-lstdc++"` from `link_libs` (or `link_flags`) from the call to `unix_cc_toolchain_config`. [1] https://bazel.build/docs/cc-toolchain-config-reference#wellknown-features [2] bazelbuild#17391 [3] bazelbuild#14342
This patch implements the `static_link_cpp_runtimes` feature in `unix_cc_toolchain_config`. The feature is documented as a well-known feature[1] and was added to the toolchain[2] but not implemented. The patch adapts a solution proposed on GitHub.[3] [1] https://bazel.build/docs/cc-toolchain-config-reference#wellknown-features [2] bazelbuild#17391 [3] bazelbuild#14342
Related Discussion: How to build cpp executable with static libsdtc++.a? -lstdc++ is hardcoded in bazel!!! Solution:
After that all executables generated by |
Description of the problem / feature request:
The
static_link_cpp_runtimes
feature doesn't link libstdc++ statically despite appearing to be intended to as per https://docs.bazel.build/versions/main/cc-toolchain-config-reference.html#well-known-features.Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
Create the following files:
BUILD:
main.cc:
WORKSPACE:
Running
bazel build //:test && ldd bazel-bin/test
gives:What operating system are you running Bazel on?
Ubuntu 22.04, but observed on several other Linuxes
What's the output of
bazel info release
?release 6.0.0-pre.20211110.1
. Usinglast_green
with bazelisk also shows the same error (when I ran this last_green was cd4bc7c).Have you found anything relevant by searching the web?
#8672 has a similar problem with fully_static_link, but this also statically links glibc, which we don't want. The solution there also depends on setting BAZEL_LINKLIBS but we'd like to be able to control which targets link libstdc++ statically individually. static_link_cpp_runtimes is also compiler-independent (if supported by the toolchain), unlike BAZEL_LINKLIBS.
The text was updated successfully, but these errors were encountered: