How to call function in .so file using dlopen() and dlsym()?

With the help of Shared libraries with GCC on Linux I tried to make and succeeded. The code runs perfectly.
But when I tried the same thing using dlopen() and dlsym() I got errors as shown below.

$ g++ -L/home/shreya/Desktop/soFiles -Wall -o test main.c -ladd1
main.c: In function ‘int main()’:
main.c:14:14: warning: variable ‘ptrVar’ set but not used [-Wunused-but-set-variable]
  pt2function ptrVar;
/tmp/ccpdIje6.o: In function `main':
main.c:(.text+0x19): undefined reference to `dlopen'
main.c:(.text+0x29): undefined reference to `dlerror'
main.c:(.text+0x54): undefined reference to `dlerror'
main.c:(.text+0x68): undefined reference to `dlsym'
main.c:(.text+0x71): undefined reference to `dlerror'
main.c:(.text+0x1ca): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status

Can any one tell me what’s wrong?

Source: gcc

Leave a Reply

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