3

I'm compiling a static library, let's call it static.a which is later linked by a shared library shared.so and by a final executable binary file (shared.so uses just a few functions from static.a maybe later this can be further splited). If I try to compile it suing gcc 7.4 I get this linker error:

/usr/bin/ld: ../../static.a(file.cpp.o): relocation R_X86_64_TPOFF32 against symbol `_ZGVZN6spdlog7details2os9thread_idEvE3tid' can not be used when making a shared object; recompile with -fPIC

I decided to try also gcc 9.1 and this error doesn't apear anymore.

  1. should I always use -fpic when building a static library that will be used in a shared library? I know fpic adds some overhead.
  2. how come a newer version of gcc can relocate the symbols of the static.a inside the shared library? Is this safe?

Thank you.

user1934513
  • 693
  • 4
  • 21

1 Answers1

0

All code in shared library should be compiled with -fPIC so your static library should too. -fPIC does indeed introduce an overhead but to a large extent it can be mitigated with options like -fno-semantic-interposition and/or -fvisibility=hidden.

The error that you see is coming from the linker so it seems that the newer GCC does not use the problematic relocation. You can inspect the generated assembly to find out the difference in generated code.

yugr
  • 19,769
  • 3
  • 51
  • 96