1

I'm using bazel to build a shared libary, but the output so file size is much bigger than expected; For comparision I also build a binary with same deps and srcs in BUILD file, the binary ouput size is much smaller; Code sample:

cc_binary(
   name = "server",
   srcs = ["server.cc"],
   deps = [...]
)  # the binary

cc_binary(
   name = "libs.so",
   srcs = ["server.cc"],
   deps = [...],
   linkshared = 1
)   # the shared library

libs.so is about 5 times larger than server; It seems linkshared option packs all symbols in deps to the shared library, including unused functions, variables(nm shows libs.so contains much more symbols than server); How can I just link needed symbols to my shared library?

1 Answer 1

1

Well, if you're compiling a shared library, there is no such thing as "unused functions". You're creating a library with functions that an executable can use.

1
  • Thanks for your answer; Finally I figured out the reason: linkshared=1 changes linker behavior by taking all object files as '--whole-archive' in -2.params file; I manually link my shared library, specifing whole/no-whole archive as needed for every object file ;
    – song xs
    Commented Aug 24, 2022 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.