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

回眸只為那壹抹淺笑 提交于 2019-11-28 20:52:36

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

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

when you later link with:

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

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

If you can't 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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!