尝试在Ubuntu上使用pthread时出错

loading...


0

我正在阅读有关C中线程的教程,并测试了以下代码:

#include <iostream>
#include <pthread.h>
#include <cstdlib>

using namespace std;

#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
   for( i=0; i < NUM_THREADS; i++ ){
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, 
                      PrintHello, &threads[i]);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

我尝试使用gcc和g编译此代码,但我总是遇到编译错误 .

使用gcc -pthread thread_test.c:

/tmp/ccmpQLyp.o:在函数PrintHello(void *)':thread_test.cpp :( . text 0x1a):undefined reference tostd :: cout'thread_test.cpp :( . text 0x1f):对std ::的未定义引用basic_ostream <char,std :: char_traits <char >>&std :: operator << std :: char_traits <char >>(std :: basic_ostream <char,std :: char_traits <char >> &&,char const ) 'thread_test.cpp :( . text 0x2e):undefined reference tostd :: ostream :: operator <<(long)'thread_test.cpp :( . text 0x33):对std :: basic_ostream的未定义引用<char,std :: char_traits <char >>&std :: endl <char,std :: char_traits <char >>(std :: basic_ostream <char,std :: char_traits <char >> &&)'thread_test.cpp :( . text 0x3b):undefined reference tostd :: ostream :: operator <<(std :: ostream&()(std :: ostream&))'/ tmp / ccmpQLyp.o:in function main':thread_test.cpp :( . text 0x63):undefined reference tostd :: cout'strread_test.cpp :( . text 0x68):对std :: basic_ostream的未定义引用<char,std :: char_traits <char >>&std :: operator << std :: char_traits <char >>( std :: basic_ostream <char,std :: char_ traits <char >> &&,char const )'thread_test.cpp :( . text 0x75):undefined reference tostd :: ostream :: operator <<(int)'thread_test.cpp :( . text 0x7a):undefined reference to std :: basic_ostream <char,std :: char_traits <char >>&std :: endl <char,std :: char_traits <char >>(std :: basic_ostream <char,std :: char_traits <char >> &&)' thread_test.cpp :( . text 0x82):undefined reference tostd :: ostream :: operator <<(std :: ostream&()(std :: ostream&))'thread_test.cpp :( . text 0xcc):undefined reference to std :: cout'thread_test.cpp :( . text 0xd1):undefined reference tostd :: basic_ostream>&std :: operator <<>(std :: basic_ostream>&,char const )'thread_test.cpp :( . text 0xde):未定义引用std :: ostream :: operator <<(int)'thread_test.cpp :( . text 0xe3):undefined reference tostd :: basic_ostream>&std :: endl>(std :: basic_ostream>&) 'thread_test.cpp :( . text 0xeb):对std :: ostream :: operator <<(std :: ostream&()(std :: ostream&))'/ tmp / ccmpQLyp.o的未定义引用:在function__static_initialization_and_destruction_0(int , 一世nt)':thread_test.cpp :( . text 0x141):未定义引用std :: ios_base :: Init :: Init()'thread_test.cpp :( . text 0x150):undefined reference tostd :: ios_base :: Init: :~Init()'/ tmp / ccmpQLyp.o :(.eh_frame 0x47):未定义引用`__gxx_personality_v0'colle2:错误:ld返回1退出状态

你能帮我吗?我是否必须做一些事情,以便此代码在Linux和Windows上运行?

1回答

  • 3

    使用 g++ 而不是 gcc ,或手动链接 -lstdc++ .

loading...

评论

暂时没有评论!