我需要动态链接我创建的库。我不确定是什么问题。一切都正确编译,但我总是将 handle
捕获为 NULL
指针:
void *handle;
char *error;
handle = dlopen ("./hw11-lib-michaelSchilling.so", RTLD_LAZY);
//same error comes up with full path as well as './hw11...'
if(!handle){
error = dlerror();
printf("%s\n", error);
printf("Error loading library.\n");
exit(1);
}
我无法通过这个错误,我不确定可能是什么地方出了问题。我很确定我已经正确编译了所有内容。以下是我使用的编译步骤:
gcc -rdynamic -c hw11-lib-michaelSchilling.c -o hw11-lib-michaelSchilling.so
gcc hw11-michaelSchilling-4.c -ldl -o hw11-michaelSchilling-4
我收到一条错误消息
only ET_DYN and ET_EXEC can be loaded.
最佳答案
在构建 hw11-lib-michaelSchilling.so
时,您似乎并没有告诉 gcc
您想要一个共享对象(.so
在名称中是不够的)。
使用 -c
,它会生成一个目标文件(不是 shared 对象)并将其命名为 michaelSchilling.so
。链接器甚至不会被调用。
从 gcc
命令行中删除 -c
并添加 -shared
:
gcc -shared -rdynamic hw11-lib-michaelSchilling.c -o hw11-lib-michaelSchilling.so
关于c - 正确使用libdl和动态链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370670/