未定义对pthread创建的引用,即使使用lpthread

标准

我想利用pthread广告,因此要使用-lpthread标志进行编译,但这是我得到的:

$ 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

我尝试编译的代码如下:

#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;
}
约翰·兹温克

可能您需要这样做:

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

这告诉编译器此标头用于C库,并且不应使用C ++名称修饰。

您还需要使用-pthread代替-lpthread,因为pthread库是特殊的,并且GCC希望明确地知道您正在尝试使用线程,而不是简单地链接到libpthread。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

即使使用了-lpthread和-ltr,也未定义引用sem_init和其他此类函数

来自分类Dev

即使使用了-lpthread和-ltr,也未定义对sem_init和其他此类函数的引用

来自分类Dev

对pthread的未定义引用

来自分类Dev

C-即使使用“ -lm”,也未定义对“ sqrt”的引用

来自分类Dev

对pthread中的运行器的未定义引用

来自分类Dev

对pthread中的运行器的未定义引用

来自分类Dev

未定义对'uuid_generate'的引用,即使我使用-luuid

来自分类Dev

即使包含库等,也未定义对PQfinish的引用

来自分类Dev

使用libav的未定义引用蚀

来自分类Dev

使用cmake对espeak的未定义引用

来自分类Dev

使用未定义的引用运行函数

来自分类Dev

未定义的引用

来自分类Dev

未定义的引用(readline,pthread_create,pthread_detach),makefile不包含库

来自分类Dev

未定义对`pthread_create'的引用使用ASIO和std :: thread制作C ++ 11应用程序时出错

来自分类Dev

C ++对动态创建的类成员的功能的未定义引用

来自分类Dev

创建函数,名称未定义且通过引用传递

来自分类Dev

对地址的未定义引用,即使在提到的行上没有引用它

来自分类Dev

即使使用isset后,未定义的变量

来自分类Dev

即使使用if语句,对象也可能未定义

来自分类Dev

即使使用isset后,未定义的变量

来自分类Dev

即使使用eval(),函数也未定义/为空

来自分类Dev

即使使用正确的版本,PHP cURL常量也未定义

来自分类Dev

g ++表示引用未定义,即使`nm`列出了我的目标文件中的符号定义

来自分类Dev

如何在构建单线程库时删除pthread未定义引用

来自分类Dev

编译boch时,对符号'pthread_create @@ GLIBC_2.2.5的未定义引用

来自分类Dev

未定义对“ pthread_key_create”的引用(链接器错误)

来自分类Dev

对'__gthrw___pthread_key_create的未定义引用(unsigned int *,void(*)(void *))

来自分类Dev

在GCC6中未定义对'rt','pthread','stdc ++ fs'的引用

来自分类Dev

编译boch时,对符号'pthread_create @@ GLIBC_2.2.5的未定义引用

Related 相关文章

  1. 1

    即使使用了-lpthread和-ltr,也未定义引用sem_init和其他此类函数

  2. 2

    即使使用了-lpthread和-ltr,也未定义对sem_init和其他此类函数的引用

  3. 3

    对pthread的未定义引用

  4. 4

    C-即使使用“ -lm”,也未定义对“ sqrt”的引用

  5. 5

    对pthread中的运行器的未定义引用

  6. 6

    对pthread中的运行器的未定义引用

  7. 7

    未定义对'uuid_generate'的引用,即使我使用-luuid

  8. 8

    即使包含库等,也未定义对PQfinish的引用

  9. 9

    使用libav的未定义引用蚀

  10. 10

    使用cmake对espeak的未定义引用

  11. 11

    使用未定义的引用运行函数

  12. 12

    未定义的引用

  13. 13

    未定义的引用(readline,pthread_create,pthread_detach),makefile不包含库

  14. 14

    未定义对`pthread_create'的引用使用ASIO和std :: thread制作C ++ 11应用程序时出错

  15. 15

    C ++对动态创建的类成员的功能的未定义引用

  16. 16

    创建函数,名称未定义且通过引用传递

  17. 17

    对地址的未定义引用,即使在提到的行上没有引用它

  18. 18

    即使使用isset后,未定义的变量

  19. 19

    即使使用if语句,对象也可能未定义

  20. 20

    即使使用isset后,未定义的变量

  21. 21

    即使使用eval(),函数也未定义/为空

  22. 22

    即使使用正确的版本,PHP cURL常量也未定义

  23. 23

    g ++表示引用未定义,即使`nm`列出了我的目标文件中的符号定义

  24. 24

    如何在构建单线程库时删除pthread未定义引用

  25. 25

    编译boch时,对符号'pthread_create @@ GLIBC_2.2.5的未定义引用

  26. 26

    未定义对“ pthread_key_create”的引用(链接器错误)

  27. 27

    对'__gthrw___pthread_key_create的未定义引用(unsigned int *,void(*)(void *))

  28. 28

    在GCC6中未定义对'rt','pthread','stdc ++ fs'的引用

  29. 29

    编译boch时,对符号'pthread_create @@ GLIBC_2.2.5的未定义引用

热门标签

归档