如何使用dlopen()和dlsym()调用.so文件中的函数



在Linux上的GCC共享库的帮助下,我尝试制作libadd1.so并成功了。代码运行完美。但是当我使用dlopen()dlsym()尝试同样的事情时,我得到了如下所示的错误。

$ 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
$
谁能告诉我出了什么事?

查看手册。对于Linux,您必须与-ldl链接才能使用动态链接功能。这有点麻烦,因为在FreeBSD上,它们是标准库的一部分。

顺便说一句,链接你想要动态打开的库(在你的情况下是-ladd1)是没有意义的。

最新更新