c++ - 修复对 dlopen() 和 dlcose() 的 undefined reference

标签 c++ linux g++ dlopen

我创建了一个简单的 C++ 应用程序。我可以编译它,它工作正常。但是现在我需要动态加载库,并且我已经将 dlfnc.h 添加到我的项目中并添加了更多代码:

#include <iostream>
#include <dlfcn.h>

void *mylib;
int eret;

using namespace std;

int main() {

    mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
    eret = dlclose(mylib);

    cout << "!!!Hello, World!!!" << endl; // Prints !!!Hello, World!!!
    return 0;
}

编译:

cd ~/workspace/LinuxGcc/src
g++ LinuxGcc.cpp

我得到一个编译错误:

/tmp/ccxTLiGY.o: In function `main':
LinuxGcc.cpp:(.text+0xf): undefined reference to `dlopen'
LinuxGcc.cpp:(.text+0x25): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status

dlfcn.h存在于/usr/include/

问题出在哪里?

最佳答案

来自 dlopen(3) :

   Link with -ldl.

所以

g++ LinuxGcc.cpp -ldl

会好的。

关于c++ - 修复对 dlopen() 和 dlcose() 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035712/

相关文章:

c++ - C++ 中的封装、数据隐藏、setter/getter

linux - ansible文件和目录权限

c++ - 如何让 g++ 需要一个标准的 `main` ?

c++ - 非类型模板参数错误 ('x' is not a type)

c++ - 来自 std::any 的 std::is_reference

c++ - 为什么 C 中的 double 打印的十进制数字比 C++ 少?

c++ - Boost套接字在断开连接时不会抛出EOF

创建具有 AX.25 封装的 TUN 网络设备

linux - 更改默认端口(22) SSh [CENTOS]

c++ - 为 g++ 永久添加包含路径和共享库