27

I have a shared library used by a another application beyond my control which requires *.so objects. My library makes use of sqlite3 which needs to be statically linked with it (I absolutely need a self-contained binary).

When I try to compile and link my library:

-fpic -flto -pthread -m64
-flto -static -shared

I end up with the following error:

/usr/bin/ld: /usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/crtbeginT.o: relocation R_X86_64_32 against `__DTOR_END__' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/crtbeginT.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

What is recompile with -fPIC related to? My code or CRT?

I have already tried to compile my object with -fPIC with the same result.

Thanks.

EDIT:

The problem does not seem to be related to SQLite3.

I wrote a simple one-line-do-nothing library which compiles and links like this:

g++ -c -fPIC -o bar.o bar.cpp
g++ -shared -o bar.so bar.o

but not like this:

g++ -c -fPIC -o bar.o bar.cpp
g++ -static -shared -o bar.so bar.o

The problem seems to be related to CRT (crtbeginT.o). Am I supposed to recompile GCC --with-pic or anything?

Petr
  • 1,128
  • 2
  • 14
  • 23
  • This is a bit confusing. Are you trying to just link your library to a static sqlite library, or are you trying to do something else as well ? – nos Jul 09 '11 at 11:21
  • PIC = Position Independent Code, required for shared libs (on most architectures, I guess) – sehe Jul 09 '11 at 11:46
  • @nos Trying to link my shared library with sqlite3.a – Petr Jul 09 '11 at 11:49
  • 3
    @Petr In that case you should at least drop the `-static` flag, and if you have a shared lib version of sqlite as well, use `-Wl,-Bstatic -lsqlite3 -Wl,-Bdynamic` to make it pick up the static version of libsqlite. – nos Jul 09 '11 at 18:37
  • @nos Thanks. -Bstatic seems to work. Don't really know why as it is supposed to be the same as -static (at least according to the manual). If you want you can write an answer so I can mark it solved. – Petr Jul 09 '11 at 21:48

3 Answers3

44

You shouldn't use the -static flag when creating a shared library, it's for creating statically linked executables.

If you only have a static version of the library, you can just link it in using -lsqlite3. But if there's both a dynamic version(.so) and a static version, the linker will prefer the dynamic one.

To instruct the linker to pick the static one, give the linker the -Bstatic flag, and make it switch back to dynamic linking for other stuff (like libc and dynamic runtime support) with -Bdynamic. That is, you use the flags:

 -Wl,-Bstatic -lsqlite3 -Wl,-Bdynamic 

Alternativly, you can just specify the full path of the .a file, e.g. /usr/lib/libsqlite3.a instead of any compiler/linker flags.

With the GNU ld, you can also use -l:libsqlite3.a instead of -lsqlite3. This will force the use of the library file libsqlite3.a instead of libsqlite3.so, which the linker prefers by default.

Remember to make sure the .a file have been compiled with the -fpic flag, otherwise you normally can't embed it in a shared library.

dubiousjim
  • 4,722
  • 1
  • 36
  • 34
nos
  • 223,662
  • 58
  • 417
  • 506
  • I've have same problem here and don't know how to compile shared library that DOES NOT dynamically links to distribution dependent staff like libc/libstdc++ - `-Bstatic` do provide dynamically linked shared library. Wonderfully, using MinGW on Windows I can link .dll with `-static` properly (depending only on windows dlls but not mingw's one). Is there a way to link .so statically??? – Nick Dec 18 '11 at 14:35
  • No. unix shared libraries work very differently from windows dlls. – nos Dec 18 '11 at 17:40
  • The -l:libsqlite3.a trick works in eclipse cdt as well. Enter :libsqlite3.a in Properties->C/C++ Build->Settings->GCC C Linker->Libraries and the build will statically link against the library. – zztops Jul 12 '13 at 19:57
  • The full path usually gets the trick done without any mumbo jumbo on which older versions of `ld` seem to fail consistently. Despite pointing it with `-L` to the right folder and using `-Bstatic -lname -Bdynamic` on the linker command line, I could not get it to work with an ancient `ld` on FreeBSD 8.4. Full path always works, however. – 0xC0000022L Dec 09 '15 at 08:55
8

Any code that will somehow make its way into a dynamic library should be relocatable. It means that everything that is linked with your .so, no matter statically or dynamically, should be compiled with -fPIC. Specifically, static sqlite library should also be compiled with -fPIC.

Details of what PIC means are here: http://en.wikipedia.org/wiki/Position-independent_code

vines
  • 5,160
  • 1
  • 27
  • 49
0

I had the same problem. Apparently -static is not the same as -Bstatic. I switched to -Bstatic and everything worked.

Dre
  • 1,985
  • 16
  • 13