Toggling --whole-archive

673 views
Skip to first unread message

eug...@tensyr.com

unread,
Feb 10, 2016, 5:23:49 PM2/10/16
to bazel-discuss
My C++ application depends on a third-party library (call it libfoo) that is compiled as "libfoo.a". I've created a cc_library rule that looks something like:

cc_library(
name = "foo",
src = ["libfoo.a"],
visibility = ["//visibility:public"],
)

My C++ binary then depends on it and it gets linked in. However I need the library to be linked with "--whole-archive" option since it contains a bunch of object files that used "__attribute__((constructor))" but are not otherwise referenced.

What I'd like the GCC to execute at link time is:

-Wl,--whole-archive libfoo.a -Wl,--no-whole-archive

but I don't know how to specify it. As far as I see, linkopts does not let me specify which options to add before and which ones after.

Is there a workaround?

Thanks,
Eugene

Brian Silverman

unread,
Feb 10, 2016, 5:38:17 PM2/10/16
to eug...@tensyr.com, bazel-discuss
Eugene,

alwayslink = True will do that.

Brian


--
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/6bc5c39a-4602-4090-b887-b7f9966982f4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eug...@tensyr.com

unread,
Feb 10, 2016, 6:13:00 PM2/10/16
to bazel-discuss, eug...@tensyr.com
On Wednesday, February 10, 2016 at 2:38:17 PM UTC-8, Brian Silverman wrote:

> alwayslink = True will do that.
>

I tried this but it doesn't seem to work. If I run "bazel build -s", the last gcc command does not have -Wl,whole-archive and it's not in the params file.

Brian Silverman

unread,
Feb 10, 2016, 6:24:11 PM2/10/16
to eug...@tensyr.com, bazel-discuss
I see; apparently (I just tried it) alwayslink only works with source and object files in the srcs of the cc_library, not other static libraries. Seems like a Bazel bug, but there might be some reason for it.

As a workaround, could you extract all the object files from the .a and then list those as srcs in the cc_library? I think you'll need to manually list them, but then you can write a genrule to extract them from the .a to feed into the cc_library.

--
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.

Ram Kumar Rengaswamy

unread,
Apr 4, 2016, 11:25:45 PM4/4/16
to bazel-discuss, eug...@tensyr.com
I am having the same problem. Pretty sure this used to work before (before bazel 0.2.0) as I have linked the gperftools profiler lib statically into a binary using linkstatic=1.

Ram Kumar Rengaswamy

unread,
Apr 4, 2016, 11:26:18 PM4/4/16
to bazel-discuss, eug...@tensyr.com
Sorry, I meant by using alwayslink=1.
Reply all
Reply to author
Forward
0 new messages