0

I have a .cpp code I wrote, which is written around a commercial software program where they provide a big c++ library class to use. I want to static link their one shared object libtdfdll.so into my executable. They do not provide me with a libtdfdll.a file just the one .so file. I am doing this in SLES 11.4 using g++ 4.8.3.

my cpp file is process_tdf_v12.5.cpp

Here is what I do now for basic dynamic linking which works:

g++ process_tdf_v12.5.cpp -I../tdflibs12.5/include -L../tdflibs12.5/lib -ltdfdll -lm

this also works for compiling/linking:

g++ process_tdf_v12.5.cpp -I../tdflibs12.5/include ../tdflibs12.5/libtdfdll.so -lm

but when I run my executable I get

error while loading shared libraries: libtdfdll.so.1: cannot open shared object file: No such file or directory

unless I do a

setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/home/ron/tdflibs12.5/lib

which I do not want to do.

I want my one executable from my process_tdf_v12.5.cpp to contain the specific version 12.5 of the tdflibs12.5/lib/libtdfdll.so so that I do not need to copy the correct 12.5 version of libtdfdll.so to other systems or worry about different .so versions causing a problem. How do I do this?

if i am given only a .so file, can static linking be done with it?

i also don't want to static link anything else, just this one libtdfdll.so file

2

It can't be done - shared libraries lack some of the information included in a static library. It looks like you've got an XY problem. You're asking about how to static link a shared library, but what you really need is a better way to distribute your application. I would recommend using something like Ermine. It bundles shared libs into one executable giving the appearance of one statically linked library

There's also flatpak. While it likely doesn't solve the problem you're having, it offers a very portable way to deliver your application plus dependencies (not just shared libs)

  • I hadn't heard of Ermine. But I wrote a tool that sounds similar (although certainly less complete): github.com/JonathonReinhart/staticx – Jonathon Reinhart Feb 28 at 14:48
  • What information is missing in a shared library? – Thayne Mar 16 at 8:23
  • @Thayne a static library is really just a bundle of objects with symbol names. When/if those objects are compiled into a shared library or executable, those symbols are replaced with real memory addresses. Additionally, the compiler may perform optimizations such as removing objects that aren't called or inlining – Outurnate Mar 19 at 6:46
  • But doesn't a shared library also have objects bundled with symbol names? Sure it's a different format, and maybe the compiler can't optimize it as efficiently, but it seems like at least in theory it should be possible to copy the (machine) code from teh shared library to a statically linked executable. – Thayne Mar 19 at 7:44
  • @Thayne yes, there is technically nothing stopping you in that regard. It is all just machine code. However, what you've effectively done is what ld does when the executable loads – Outurnate Mar 19 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.