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

喜欢而已 提交于 2019-12-17 23:13:52

问题


I have to link my code to a shared library without the lib prefix. (say, foo.so) The first problem is -l option does not find the file. So I tried directly including this file to the last compilation like this:

gcc a a.o /PATH/TO/FOO/foo.so

But in this case, a is hard linked to foo.so as an absolute path as seen in "ldd a":

/PATH/TO/FOO/foo.so

In the final deployment both files would end up being in the same folder, so this should be normal link, not the absolute path. How can I do this?


回答1:


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



回答2:


-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.



来源:https://stackoverflow.com/questions/1305266/how-to-link-to-a-shared-library-without-lib-prefix-in-a-different-directory

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