Linker Flag `-Wl,--as-needed`

401 views
Skip to first unread message

UBC Thunderbots

unread,
Mar 11, 2020, 5:46:37 AM3/11/20
to bazel-...@googlegroups.com
Hello,

We are using bazel to build a large cpp project with gcc: https://github.com/UBC-Thunderbots/Software. In particular, the linux_gcc toolchain. We build targets that have dependencies on other targets, who in turn depend on other libraries or targets. This requires `-Wl,--as-needed` flag to be put before the -l[targets] that a target depends on. For example, (from a .params file)

===
-o
bazel-out/k8-fastbuild/bin/software/new_geom/polygon_test
-Lbazel-out/k8-fastbuild/bin/_solib_k8
-Lbazel-out/k8-fastbuild/bin/_solib__cc_Utoolchain_Ccc_Utoolchain_Ulinux_Ugcc_U7_U4_U0
bazel-out/k8-fastbuild/bin/software/new_geom/_objs/polygon_test/polygon_test.pic.o
[Insert `-Wl,--as-needed` here]
-lsoftware_Snew_Ugeom_Slibpolygon
-lsoftware_Snew_Ugeom_Slibpoint
-lsoftware_Snew_Ugeom_Slibvector
-lexternal_Sboost_Slibchrono
-lexternal_Sboost_Slibsystem
-lexternal_Sboost_Slibcontainer
-lexternal_Sboost_Slibregex
-lexternal_Sboost_Slibexception
-lexternal_Sgtest_Slibgtest_Umain
-lexternal_Sgtest_Slibgtest
-lstdc++
-static-libgcc
-Wl,-S
-Wl,-z,relro,-z,now
-Wl,--build-id=md5
-Wl,--hash-style=gnu
-pthread
-std=c++17
-Wno-unused-command-line-argument
-lm
-lpthread
-ldl
-lrt
-fuse-ld=gold
-Wl,-rpath,$ORIGIN/../../_solib_k8/
-Wl,-rpath,$ORIGIN/../../_solib__cc_Utoolchain_Ccc_Utoolchain_Ulinux_Ugcc_U7_U4_U0/
-Wl,-rpath,$ORIGIN/_solib__cc_Utoolchain_Ccc_Utoolchain_Ulinux_Ugcc_U7_U4_U0/

===
The only way we’ve been able to do this is by adding it to the end of the gcc path in src/cc_toolchain/wrapper/linux_gcc-gcc. We don’t think this is an elegant solution and that there must be a way to add the `-Wl,--as-needed` through other options.

Please let us know what you think and what are the possible solutions.

Thanks,
---

UBC Thunderbots
Faculty of Applied Science
The University of British Columbia | Vancouver Campus 


Greg Estren

unread,
Mar 16, 2020, 3:59:51 PM3/16/20
to UBC Thunderbots, bazel-discuss
Hi,

I think configuring the toolchain directly to add those linker flags would be the right approach.  I'm not up-to-date with the latest C++ toolchain format, so I can't give you precise guidance. But I think something along the lines of https://github.com/bazelbuild/bazel-toolchains/blob/a7fe422b2bba8bcf2291eaa77df86c6077da6766/configs/ubuntu16_04_clang/9.0.0/bazel_0.28.1/cc/BUILD#L103 and  https://github.com/bazelbuild/bazel-toolchains/blob/a7fe422b2bba8bcf2291eaa77df86c6077da6766/configs/ubuntu16_04_clang/9.0.0/bazel_0.28.1/cc/cc_toolchain_config.bzl#L176 would get you in the right direction.

Someone closer to C++ configuration could condense this into a simpler minimal example.

--
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/CAH2kcUvDFJ%3DctE0zBUoYSmObwygpu-5DU9BJDNw9oKnkSVyKjA%40mail.gmail.com.

UBC Thunderbots

unread,
Mar 21, 2020, 3:47:58 PM3/21/20
to Greg Estren, bazel-discuss
Hey,

Thank you for the suggestion! Unfortunately it seems that -Wl,--no-as-needed needs to come before any libraries are linked in, and flags set in the toolchain config appear to come after. For example, adding the following feature:

    force_linking = feature(
        name = "force_linking",
        flag_sets = [
            flag_set(
                actions = ALL_LINK_ACTIONS,
                flag_groups = [flag_group(flags = [
                    "-Wl,--no-as-needed",
                ])],
            ),
        ],
    )

results in the aforementioned linking errors.
---

UBC Thunderbots
Faculty of Applied Science
The University of British Columbia | Vancouver Campus 



Reply all
Reply to author
Forward
0 new messages