Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LD_LIBRARY_PATH is ignored by GCC

Tags:

c

linux

gcc

according to docs, GCC looks paths in LD_LIBRARY_PATH for linking shared library BUT it seems in my case it is ignored!

echo $LD_LIBRARY_PATH -->:/home/mehrdad/usr/lib (so LD_LIBRARY_PATH is set currectly)

i have libfoo.so in "/home/mehrdad/usr/lib" BUT : gcc main.c -lfoo returns error :

/usr/bin/ld: cannot find -lfoo
collect2: error: ld returned 1 exit status

so what is the problem??? is LD_LIBRARY_PATH deprecated???!

but i can successfully link with explicit command :

gcc main.c -L/home/mehrdad/usr/lib -lfoo

and also I can successfully execute the a.out by just:

./a.out

it seems LD_LIBRARY_PATH is respected by OS library loader BUT NOT GCC!

my environment : OS : CentOs 7 compiler : gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)

like image 859
mehrdad Avatar asked Oct 24 '25 08:10

mehrdad


1 Answers

I was wrong! thanks to Alexandre C and David Schwartz!

LD_LIBRARY_PATH is only for loader(runtime).

LIBRARY_PATH is what I need according to the docs:

The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it cannot find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).

like image 112
mehrdad Avatar answered Oct 26 '25 23:10

mehrdad