Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to link to a shared library without access to the library itself?

I have the header files of a shared library but not the shared library nor its source code.

Can I still compile some code against this library?

If not, what information does the shared library contain which is not in the headers?

like image 211
Georg Schölly Avatar asked Jan 30 '23 01:01

Georg Schölly


1 Answers

Can I still compile some code against this library?

Compile: yes. Link: maybe.

You can create a dummy library to link against. E.g. if the header contains:

int library_func(void*);

then:

// dummy_lib.c
int library_func(void *p) { return 0; }

gcc -fPIC -shared -o libfoo.so dummy_lib.c

# Now you can use libfoo.so to link your program.

There are some gotcha's:

  1. The real library may have SONAME of something other than libfoo.so (e.g. libfoo.so.2). There is no way for you to know without access to the real libfoo.
  2. The real library could use versioned symbols. If you link your program against the dummy library, it will use default version of all referenced symbols, which is likely to be correct now, but is likely to break in the future (if/when the real library is updated with a new and incompatible implementation of any symbols you call).
like image 73
Employed Russian Avatar answered Feb 03 '23 12:02

Employed Russian