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
file1.soand (b) the command line used to build the executable. Both are relevant because you might have built the information thatfile1.soneedsfile2.sointofile1.sowith the answer to (a), and in any case, there are ways to avoid the problem in (b). Note that it is normal for functions in one shared library (e.g.file2.so) to call on functions in another shared library (e.g.libc.so). Or considerlibncurses.sowhich useslibc.so; and so on.file1.so; your comment discusseslibfile1.so. There's a lot of difference between the naming conventions. Thelibprefix permits the use of-lfile1notation on the linker command line. Without that prefix, you can't use the shorthand; you must specify the path to the shared object on the link line. There is also mention of dynamic loading in a comment to the answer. That should be part of the question too; it changes things a lot, too.