开发者

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

开发者 https://www.devze.com 2023-01-10 03:05 出处:网络
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

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)


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.


you can set the LIBRARY_PATH environment variable.

export LIBRARY_PATH=/home/user/ba
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号