gcc Linkage option -L: Alternative ways how to specify the path to the dynamic library

谁说我不能喝 提交于 2019-12-22 10:20:42

问题


If I compile my source code with "-L." the dynamic library libmd5.so can be found.

gcc main.c -g -O2 -Wall -o main -L. -lmd5 -lcr

But if I leave the "-L."-option away, the linker does not find the dynamic library. How can I change that without having to invoke "-L."?

(additional info libmd5.so and libmd5.so.1.0.1 are located in /home/user/ba)


回答1:


There's really nothing wrong with the -L flag, so you shouldn't try so hard to get rid of it - is it at runtime you have problems, as the system won't load the libraries you link to ? Here's some options :

  • Add /home/user/ba to /etc/ld.so.conf (or similar for your OS) and run ldconfig afterwards. This will be system wide.
  • Set the LIBRARY_PATH(for link time) and LD_LIBRARY_PATH(for run time) environment variable. export LD_LIBRARY_PATH=/home/user/ba and export LIBRARY_PATH=/home/user/ba .This will have effect only for the current shell.
  • Set the rpath in the executable (you still need the -L . here though). Add -L /home/user/ba -Wl,-rpath,/home/user/ba to your linker flags. This will have effect only for the executable you're making.
  • Place your shared libraries in a system wide library search path such as /usr/lib. This will be system wide.

The above have effect at runtime as well - it'll try to find libmd5.so in /home/user/ba or other library search paths for the system when you run the app as well.




回答2:


you can set the LIBRARY_PATH environment variable.

export LIBRARY_PATH=/home/user/ba


来源:https://stackoverflow.com/questions/3374767/gcc-linkage-option-l-alternative-ways-how-to-specify-the-path-to-the-dynamic-l

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