What is the use of -Wl,-rpath in while making the executable from the library

We know that the command for making the executable is :

gcc -L/home/username/foo -Wall -o test main.c -lfoo

But if run ./test ,it will give error :

/test
./test: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory

There are 2 solutionss for it:
1)

 LD_LIBRARY_PATH=/home/username/foo

2)

gcc -L/home/username/foo -Wl,-rpath=/home/username/foo -Wall -o test main.c -lfoo

my question is why do need to provide the library path to the loader??While making the execuatble I have explicilty mentioned the path such that the linker could create the excutable and I beleive there must be a refernce of the location of the library embedded in th execuatble

This a conceptual doubt i have ,plz clear it out.Thank you


Source: gcc

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.