0

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?

Community
  • 1
  • 1
Everyone
  • 1,751
  • 13
  • 36

2 Answers2

1

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

Keith k
  • 63
  • 7
  • Thanks this resolved the loading issue, however, i am facing another issue with `std::thread t(&TextSearch::ThreadProc, this);`. It causes runtime error as it doesnt like being treated not as `pthread`. Any clue? – Everyone Mar 20 '17 at 22:02
  • Thanks for the answer, Rama's answer fixed everything for me – Everyone Mar 21 '17 at 15:40
  • @Everyone Happy coding friend – Keith k Mar 21 '17 at 20:42
1

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;
}
Rama
  • 3,222
  • 2
  • 11
  • 26