1

I want to make use of pthread ad hence use the -lpthread flag to compile, but here's what I get:

$ g++ -lpthread pseudo_code.cpp 
/tmp/cc3mPrvt.o: In function `MyThreadClass::StartInternalThread()':
pseudo_code.cpp:(.text._ZN13MyThreadClass19StartInternalThreadEv[_ZN13MyThreadClass19StartInternalThreadEv]+0x26): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

The code I try to compile is below:

#include <pthread.h>
#include <iostream>
#include <vector>
#define OK      0
#define ERROR   -1

//-- ThreadClass
class MyThreadClass
{
public:
   MyThreadClass() {/* empty */}
   virtual ~MyThreadClass() {/* empty */}

   /** Returns true if the thread was successfully started, false if there was an error starting the thread */
   bool StartInternalThread()
   {
      return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
   }

   /** Will not return until the internal thread has exited. */
   void WaitForInternalThreadToExit()
   {
      (void) pthread_join(_thread, NULL);
   }

protected:
   /** Implement this method in your subclass with the code you want your thread to run. */
   virtual void InternalThreadEntry() = 0;

private:
   static void * InternalThreadEntryFunc(void * This) {
       ((MyThreadClass *)This)->InternalThreadEntry(); return NULL;
       }

   pthread_t _thread;
};
//-- /ThreadClass
//--- DUMMY DECLARATIONS BELOW TO MAKE IT COMPILE ---//
#define LOG_NS_ERROR std::cout
class test{
    public:
        int get_child(std::string x){return OK;};
};
test *_global;
typedef struct test_struct{} _db_transact;
class db_transact{
    public: 
        db_transact(int,int&,int&){};
};
int _ns;
int _log_id;
//--- DUMMY DECLARATIONS ABOVE TO MAKE IT COMPILE ---//
class db_c_hndlr : public MyThreadClass{
    public: 
        db_c_hndlr(void);
        ~db_c_hndlr(void);
        db_transact *db_conn_get(void);
        void InternalThreadEntry(void *func);
    private:
        int _stop;
        std::vector<db_transact*> _db_pool;
};
//---------------------------------------------------------

db_c_hndlr::db_c_hndlr(void) {
}
//---------------------------------------------------------

void db_c_hndlr::InternatThreadEntry(void *func) {

    while(!stop){
        std::cout << "going!" << std::endl;
        sleep(1);
    }
}
//---------------------------------------------------------

db_c_hndlr::~db_c_hndlr() {
    int i = 0;
    std::vector<db_transact*>::iterator it;
    for (i=0, it = _db_pool.begin();it!=_db_pool.end();it++, i++) {
        if (_db_pool[i])
            if (_db_pool[i]!=NULL) 
                delete _db_pool[i];
    }
}
//---------------------------------------------------------

db_transact *db_c_hndlr::db_conn_get(void) {
    db_transact *tmp;

    tmp = new db_transact(_global->get_child("db_config"), _ns, _log_id);
    _db_pool.push_back(tmp);
    return tmp;
}
//---------------------------------------------------------
int main(void)
{
    db_transact *conn=NULL;
    db_c_hndlr db;
    //db = new db_c_hndlr();

    conn= db.db_conn_get();
    return OK;
}
stdcerr
  • 13,725
  • 25
  • 71
  • 128

2 Answers2

1

Probably you need to do this:

extern "C" {
#include <pthread.h>
}

That tells the compiler that this header is for a C library, and that it should not use C++ name mangling.

You also need to use -pthread instead of -lpthread, because the pthread library is special and GCC wants to explicitly know you are trying to use threads, not simply link against libpthread.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Please try to compile with the command.

g++ pseudo_code.cpp -lpthread

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

It worked for me. It seems, needs to specify the library after the source file so that symbols are searched in the library.

Preeti
  • 171
  • 4