正确使用libdl和动态链接库

Mic*_*ing 10 c dll shared-libraries

我需要动态链接我创建的库.我不确定问题是什么.这一切编译正确,但我总是赶上handleNULL指针:

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);
}
Run Code Online (Sandbox Code Playgroud)

我无法通过这个错误,我不确定什么可能是错的.我很确定我已经正确编译了所有内容.以下是我使用的编译步骤:

gcc -rdynamic -c hw11-lib-michaelSchilling.c -o hw11-lib-michaelSchilling.so
gcc hw11-michaelSchilling-4.c -ldl -o hw11-michaelSchilling-4
Run Code Online (Sandbox Code Playgroud)

我收到的错误是读取的

只能加载ET_DYN和ET_EXEC.

NPE*_*NPE 19

在构建时hw11-lib-michaelSchilling.so,您似乎并没有告诉gcc您需要共享对象(.so名称中的名称是不够的).

随着-c它生成一个目标文件(不是共享对象)并调用它michaelSchilling.so.链接器甚至不会被调用.

删除-cgcc命令行并添加-shared:

gcc -shared -rdynamic hw11-lib-michaelSchilling.c -o hw11-lib-michaelSchilling.so
Run Code Online (Sandbox Code Playgroud)