views:

40

answers:

2

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  A: 

you can set the LIBRARY_PATH environment variable.

export LIBRARY_PATH=/home/user/ba
eduffy
+2  A: 

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.

nos
Thank you. I did not really wanted to get rid of it. But I did not know how to change automake so that the "-L." is included when I compile my project with Anjuta.
OldMacDonald