[ACCEPTED]-How to link to a shared library without lib* prefix in a different directory?-shared-libraries

Accepted answer
Score: 15

Assuming an ELF platform, if you can rebuild 5 foo.so:
- the best fix is to simply name it libfoo.so
- the 4 next best fix is to set SONAME on it:

  gcc -Wl,-soname,foo.so -o foo.so foo.o

when you 3 later link with:

  gcc -o a.out a.o /path/to/foo.so

only the SONAME will be recorded 2 as a dependency, not a full /path/to/foo.so.

If you can't 1 rebuild foo.so, then do this:

  rm -f foo.so && ln -s /path/to/foo.so foo.so &&
  gcc -o a.out a.o ./foo.so && rm -f foo.so
Score: 7

-Wl,-rpath,. --> to use current directory 5 for searching lib files. (even if not found 4 in compilation, ok at run-time) instead 3 of -llibrary --> use library.so.

This seems 2 to work correctly. Hope anyone finds this 1 useful.

More Related questions