简体   繁体   中英

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"

Inside the library, it creates a thread . I am using C++11 threads for this:

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

As I said, the library compiles and I have the libTextSearch.so file.

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. 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. Typically in your build environment you would supply -lpthread as an argument to dynamically link to it.

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

for example

Just dlopen thread library beforehand with 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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