0

Today, When trying to build my so lib project with mongodb c++ client, I got the error:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../libmongoclient.a(connection_factory.o): relocation R_X86_64_32S against `_ZTVN5mongo17AScopedConnectionE' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../libmongoclient.a: error adding symbols: Bad value
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I googled -fPIC, but got nothing. Where can I find the doc about this? What's this? I am using clang++ for building.

Dean Chen
  • 3,800
  • 8
  • 45
  • 70
  • Does this answer your question? [GCC -fPIC option](https://stackoverflow.com/questions/5311515/gcc-fpic-option) – Zoe Sep 24 '20 at 14:26

3 Answers3

7

PIC stands for Position Independent Code. Quoting from man gcc:

If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table.

carlosdc
  • 12,022
  • 4
  • 45
  • 62
3

You compiled shared library without having relocatable code turned on at compile time. It is strongly suggested to use position independent code (PIC or PIE) when building shared libraries.

Please refer http://en.wikipedia.org/wiki/Position-independent_code for more details.

nyrl
  • 769
  • 5
  • 15
1

There is a bug in this system,you can not use .o or .a compiled intermediate file to generate dynamic lib(xx.so file),you may try to directly use the .cpp or .c file to generate a dynamic lib,also you may see this link to fix this bug (link site)

Community
  • 1
  • 1
pinnace
  • 71
  • 3