C++ Linux 加载共享库时出错`undefined symbol: pthread_create`

C++ Linux Error Loading Shared Library `undefined symbol: pthread_create`

我创建了一个编译良好的库。 库文件是"libTextSearch.so"

在库中,它创建了一个 thread。我为此使用 C++11 线程:

TextSearch::TextSearch(){
    std::thread t(&TextSearch::ThreadProc, this);
    t.detach();
}

正如我所说,库编译并且我有 libTextSearch.so 文件。

我正在尝试在不同的应用程序中加载库:

void* handle = dlopen("libTextSearch.so", RTLD_LAZY);
if(!handle){
    //std::cout << "\n  Failed to load libTextSearch.so\n\n";
    fprintf(stderr, "dlopen failed: %s\n", dlerror());
    return 1;
}

我已经将包复制到 /usr/lib。这是我得到的输出:

dlopen failed: /usr/lib/libTextSearch.so: undefined symbol: pthread_create
RUN FINISHED; exit value 1; real time: 0ms; user: 0ms; system: 0ms

我查过这个question。我认为这是相关的,但我不知道将其应用于我的情况。

有什么想法吗?

我不能完全确定,因为我不知道你是如何构建这个项目的,或者 libTextSearch.so 是如何构建的,但是你需要在生成 libTextSearch 时以某种方式 link 反对 libpthread .通常在您的构建环境中,您会提供 -lpthread 作为动态 link 的参数。

gcc -c testsearch.cpp -lpthread -o textsearch.o 

例如

只需 dlopen thread 图书馆预先 RTLD_GLOBAL

void* handlePthread = dlopen("libpthread.so.0", RTLD_GLOBAL | RTLD_LAZY);
if(!handlePthread ){
    //std::cout << "\n  Failed to load libpthread.so.0\n\n";
    fprintf(stderr, "dlopen failed: %s\n", dlerror());
    return 1;
}