小编Lor*_*kos的帖子

为什么这个简单的 Linux C 程序在运行时加载 .so 会崩溃?

我正在尝试编写在运行时加载我的共享对象 (.so) 的最小程序。

不幸的是,尽管进行了错误检查,它还是在运行时挂起 :-(

我对我在源代码级别忽略的内容非常感兴趣。

源代码和运行我的程序的 shell 会话如下。

文件“libsample.c”:

#include <stdio.h>

void sample_check(void)
{
    printf("INFO: Function sample_check() called.\n");
}
Run Code Online (Sandbox Code Playgroud)

文件“test.c”:

#include <stdio.h>
#include <dlfcn.h>

typedef void (*sample_func_t) (void);

int main(void)
{
    setbuf(stdout, NULL);
    setbuf(stderr, NULL);
    void* h_lib = dlopen("./libsample.so.1", RTLD_LAZY); // RTLD_LAZY || RTLD_NOW
    if (! h_lib)
    {
        fprintf(stderr, "ERROR(%d): %s\n", __LINE__, dlerror());
        return 1;
    }

    sample_func_t* symver = NULL;
    dlerror();
    symver = dlsym(h_lib, "sample_check");
    char* reter = dlerror();
    if (reter)
    {
        fprintf(stderr, "ERROR(%d): %s\n", __LINE__, reter);
        return 1; …
Run Code Online (Sandbox Code Playgroud)

c linux function-pointers shared-libraries dlsym

5
推荐指数
1
解决办法
413
查看次数

标签 统计

c ×1

dlsym ×1

function-pointers ×1

linux ×1

shared-libraries ×1