MD. Nazmul Kibria
MD. Nazmul Kibria

Reputation: 1090

Alternate to "`--whole-archive`" in bazel

I want to link an external static lib in one of my bazel based c++ project. I need "whole-archive" option for linking the library like gcc or g++ build:

g++ main.cc -Wl,--whole-archive -lhttp -Wl,--no-whole-archive

Can anybody suggest what is the alternate to "--whole-archive" in bazel?

Upvotes: 3

Views: 3160

Answers (3)

FDS
FDS

Reputation: 5397

Have had success with

cc_library(
  name = "hello",
  ..
  linkstatic = True,
  alwayslink = True,
)

To verify, check an executable that links your library. The file bazel-bin/path/to/my_executable-2.params will show

-Wl,-whole-archive
bazel-out/.../path/to/libhello.lo
-Wl,-no-whole-archive

I seem to remember this working for .so (dynamically linked) libraries as well, but cannot confirm at the moment.

Upvotes: 1

hlopko
hlopko

Reputation: 3268

Sadly, alwayslink doesn't work with precompiled libraries, only with cc_library compiled and linked by Bazel. There is one undocumented hack (I guess I'm just documenting it by mentioning it here), and it's to rename .a file to .lo file. Then Bazel will link it as whole archive.

Beware that this is a hack, and will stop working without warning. We have plans for some variation of cc_import rule exactly for this use case, to import a precompiled binary into the workspace with the ability to set whole archiveness on it. It's just not there yet.

Upvotes: 3

Austin Schuh
Austin Schuh

Reputation: 53

https://bazel.build/versions/master/docs/be/c-cpp.html#cc_library.alwayslink

alwayslink

Boolean; optional; nonconfigurable; default is 0

If 1, any binary that depends (directly or indirectly) on this C++ library will link in all the object files for the files listed in srcs, even if some contain no symbols referenced by the binary. This is useful if your code isn't explicitly called by code in the binary, e.g., if your code registers to receive some callback provided by some service.

Upvotes: 0

Related Questions