'Compiling dynamically linked library in a makefile

I am trying to run my library using a make file. I currently have a dynamic library called libname.so which I created by linking the object files of some of my C files. This library works correctly when I run the following lines of code in my linux shell :

   gcc -L. main1.c -lname -o out
   LD_LIBRARY_PATH=.
   export LD_LIBRARY_PATH

But when I copy these exact lines of code in to a make file and name the make file title for this function 'names' and then run 'make names' in linux shell, I get the following error:

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

Then once again when I run the final two lines of code shown at the end of the makefile function again then run the out file, it is fixed and the program works again.

I just need to figure out how to make it work directly from the makefile.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source