[ACCEPTED]-Is it possible to statically link against a shared object?-static-linking

Accepted answer
Score: 10

There's a fundamental difference between 16 a shared library and a static library. First 15 off, do search this site for previous discussions, and 14 check out this question too (and the answers therein).

Basically, a 13 static library is just a collection of objects, and 12 the linker resolves the symbol names into 11 fixed addresses -- this is required for 10 static linking. On the other hand, a shared 9 library is much more like an independent 8 executable, which is loaded into memory 7 by the loader and has entry point addresses 6 to which the program jumps. However, relocation 5 tables that static libraries have are generally 4 not preserved when a shared library is being 3 linked, so it's in general not possible 2 to extract linkable object code from inside 1 a linked shared library.

Score: 4

Yeah, I know this is an 6 year-old question. I 12 was told that it was possible to statically 11 link against a shared-object library, but 10 I've also discovered that it is not.

To actually 9 demonstrate that statically linking a shared-object 8 library is not possible with ld (gcc's linker), use 7 the following gcc command:

gcc -o executablename objectname.o -Wl,-Bstatic -l:libnamespec.so

(Of course you'll have to compile objectname.o from sourcename.c, and you should probably make up your own shared-object library as well. If you do, use -Wl,--library-path,. so that ld can find your library in the local directory.)

The actual error 6 you receive is:

/usr/bin/ld: attempted static link of dynamic object `libnamespec.so'
collect2: error: ld returned 1 exit status

Clearly, attempting to pull 5 the object out of the shared-object library 4 is something about which ld will balk.

There 3 were some suggestions made here, but my interest 2 in this question was merely academic.

Hope 1 that helps.

More Related questions