欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

undefined reference to `dlopen'

程序员文章站 2022-07-08 14:23:29
...

在编译使用 dlfcn.h 的cpp文件时报错:

/tmp/ccd4NLWa.o: In function `main':
demo.cpp:(.text+0x27): undefined reference to `dlopen'
demo.cpp:(.text+0x37): undefined reference to `dlerror'
demo.cpp:(.text+0x7c): undefined reference to `dlsym'
demo.cpp:(.text+0x85): undefined reference to `dlerror'
demo.cpp:(.text+0xc7): undefined reference to `dlclose'
demo.cpp:(.text+0x184): undefined reference to `dlsym'
demo.cpp:(.text+0x18d): undefined reference to `dlerror'
demo.cpp:(.text+0x1cf): undefined reference to `dlclose'
demo.cpp:(.text+0x320): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status

解决办法
编译的时候加上 -ldl, 比如:

g++ -o demo demo.cpp -I./include -ldl

原因 是虽然导入了相应的头文件,但并没有加上libdl.so 联合编译。