I have some very special needs:
Is is possible to link a .so
file statically to a built executable so that the executable doesn't link this .so
dynamically any more?

- 223,805
- 18
- 296
- 547

- 2,817
- 9
- 22
-
very specific needs? Bonita, just google "ldd static linking how to" – Oleksandr Kravchuk Jun 20 '16 at 14:12
-
I'm not sure you understood me right. I want as solution to statically link an already linked excecutable to a set of its dynamic dependencies. Its not possible to find a howto for that problem with the search-string you mention. – Bonita Montero Jun 20 '16 at 15:15
-
It is extremely easy to find a good how-to – Oleksandr Kravchuk Jun 20 '16 at 15:19
-
http://stackoverflow.com/questions/4156055/gcc-static-linking-only-some-libraries – Oleksandr Kravchuk Jun 20 '16 at 15:20
-
1You don't understand the question. But I got a hint on two tools which do what my friends needs: ELF Statifier (http://statifier.sourceforge.net/) and Ermine (http://www.magicermine.com/) – Bonita Montero Jun 21 '16 at 08:20
-
Please **edit your question** to improve it and motivate it. – Basile Starynkevitch Jul 06 '16 at 11:01
-
Badly smells as an [XY problem](http://xyproblem.info/). You really should give motivations. – Basile Starynkevitch Jul 06 '16 at 11:37
-
I don't know OP's motivation, but for me it is making a pre-compiled binary portable between systems. – Robert Lugg Oct 09 '19 at 20:45
1 Answers
An ELF shared object should be somehow dynamically linked and should practically contain position-independent code (this is strongly recommended, but in theory not mandatory). See also this
For all the details, read Drepper's paper How To Write Shared Libraries (more than 100 pages).
Read also the Program Library HOWTO and the C++ dlopen minihowto.
Perhaps you simply want to have the executable be able to do something sensible if the shared library is missing. Then you might simply load it explicitly (as a plugin) at runtime using dlopen(3), handle nicely the failure error case, and on success get the appropriate symbols from it using dlsym(3). See also ld-linux.so(8), elf(5), execve(2), mmap(2)
Perhaps you want to have an entirely static ELF executable. Then you should build it as such (and use static libraries only).
Maybe you want to extract the text, data, and relocation info from the shared object (that might be painfully doable using objdump
, readelf
, or some code using libbfd
or libelf
) and rebuild some linkable object file (perhaps with ld -r
or some painful ld
script). I am not sure it is entirely doable in all cases (e.g. when the .so
has dynamic dependencies itself), and it is certainly painful.
PS. Please motivate your question a lot more and explain why you are really asking. I made some blind guesses. I suppose you have access to source code and are able to change some of it.

- 1
- 1

- 223,805
- 18
- 296
- 547