简体   繁体   English

C ++ Linux错误正在加载共享库`undefined symbol:pthread_create`

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

I have created a library that compiles well and everything. 我创建了一个可以很好地编译所有内容的库。 The library file is "libTextSearch.so" 库文件是“ libTextSearch.so”

Inside the library, it creates a thread . 在库内部,它创建一个thread I am using C++11 threads for this: 我为此使用C ++ 11线程:

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

As I said, the library compiles and I have the libTextSearch.so file. 如我所说,库已编译,并且我有libTextSearch.so文件。

I am trying to load the library in a different application: 我正在尝试在其他应用程序中加载库:

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;
}

I have the package copied to /usr/lib already. 我已经将软件包复制到/usr/lib This is the output I get: 这是我得到的输出:

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

I have looked up this question . 我查了这个问题 I think it is related, but I have no idea of applying that to my situation. 我认为这是相关的,但我不知道将其应用于我的情况。

Any ideas? 有任何想法吗?

I can't exactly be sure since I don't know how you are building this project, or how libTextSearch.so is built, but you need to link against libpthread somehow when generating libTextSearch. 我不确定,因为我不知道您如何构建此项目,也不知道libTextSearch.so的构建方式,但是在生成libTextSearch时,您需要以某种方式链接到libpthread。 Typically in your build environment you would supply -lpthread as an argument to dynamically link to it. 通常,在您的构建环境中,您将提供-lpthread作为参数以动态链接到它。

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

for example 例如

Just dlopen thread library beforehand with RTLD_GLOBAL 只需使用RTLD_GLOBAL预先dlopen thread

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM