C ++ Linux Error加载共享库`undefined symbol:pthread_create`

时间:2017-03-20 19:20:24

标签: c++ linux c++11 pthreads shared-libraries

我创建了一个可以很好地编译的库。 库文件是" 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。我认为这是相关的,但我不知道应用于我的情况。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我无法确定,因为我不知道你是如何构建这个项目的,或者是如何构建libTextSearch.so的,但是你需要在生成libTextSearch时以某种方式链接libpthread。通常在构建环境中,您将提供-lpthread作为参数以动态链接到它。

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

例如

答案 1 :(得分:1)

预先使用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;
}