-L and shared libraries

Thomas Lavergne thomasl@met.no
Fri Nov 16 17:08:00 GMT 2007


Hi all,

    I bumped into an issue this afternoon (european time) with gcc and the linking of dynamic 
libraries. It is not the first time I run into it but this time I will try to get an answer from you 
gurus. I browsed the archive for this list and the closest I could get was a message in 2003, 
looking like:

************************
 > We want to set up gcc to search /usr/local/GCC/lib when compiling
 > *without* having to set up LD_LIBRARY_PATH.  Is there a way of doing
 > this?

  -Ldir Add directory dir to the list of directories to be searched for
        `-l'.
*************************
I do not fully agree with the answer. It seems to me that -Ldir answers to only part of the problem 
(on my Linux-x86 configutation, at least).

I hope confirmation or pointing to section of the man pages :-)

My situation is,
 > gcc bar.o -o bar.exe -lfoo
does work because foo.so is in the default search path (/usr/lib/libfoo.so exists).

But I want to use the version of foo which is in /usr/local/GCC/lib. So I use:

gcc -L/usr/local/GCC/lib bar.o -o bar.exe -lfoo

This also compiles smoothly. I note that it would also compile ok if there was no libfoo.so in the 
/usr/local/GCC/lib. There would also be no further problem if both versions of libfoo was static 
objects.

*But*, coming back to my .so case, if I try
 > ldd bar.exe
I see that the reference to foo.so is in /usr/lib. :-o.
I have to 1) either set the LD_RUN_PATH but it is too tedious as I have several other libraries and 
I am lazy or 2) use:

 > gcc -L/usr/local/GCC/lib bar.o -Wl,-rpath=/usr/local/GCC/lib -o bar.exe -lfoo
Then ldd gives ok result.

So now my questions:
1) what controls this behaviour (that the dynamic loader still tries to load foo.so from the default 
directories)? Is it a feature of gnu ld? Is it different on other architecture?
2) should not the semantic of -L be exactly that? If you are to link against a dynamic library, pass 
-rpath=libdir to the linker.
3) could we imagine a new flag (-LLdir) that would automatically pass the -rpath=dir info to ld (of 
course only if libfoo is a dynamic libary)? That would be a cool feature, wouldn't it?


Et voila.
Sorry if it is FAQ or solved elsewhere. I definitely wait for some enlightments...
Thomas

PS: Please answer to me as I am not subscribed to the list.





More information about the Gcc-help mailing list