I have a shared library say libfile2.so (which contains print2() function definition). Now I create a libfile1.so (which contains print1() function definition which in turn calls print2() function in libfile2.so). Now I create a main.c file which contains main() function which calls print1() by dynamically linking libfile1.so.
But I am getting the following error:
./libfile1.so: undefined reference to `print2'**
The following are the commands that I am using:
gcc -c -fpic file1.c
gcc -shared -o libfile1.so file1.o
gcc -c -fpic file2.c
gcc -shared -o libfile2.so file2.o
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
gcc -I. -L. -o main main.c -lfile1
If you have called only
print1in yourmain.c. Then set the path of thelibfile2.soin theLD_LIBRARY_PATH. Because it will try to find the dependencies oflibfile1.sowhile linking withmain.c.If you have called both
print1andprint2inmain.cthen link bothlibfile1.soandlibfile2.solike below.Because all the symbol used in main.c needs to be resolved while generating executable.