我閱讀用C線程++教程和測試下面的代碼:錯誤嘗試使用並行線程在Ubuntu
#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: In function
PrintHello(void*)': thread_test.cpp:(.text+0x1a): undefined reference to
std::cout' thread_test.cpp:(.text+0x1f): undefined reference tostd::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 to
std::ostream::operator<<(long)' thread_test.cpp:(.text+0x33): undefined reference tostd::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 to
std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In functionmain': thread_test.cpp:(.text+0x63): undefined reference to
std::cout' thread_test.cpp:(.text+0x68): undefined reference tostd::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 to
std::ostream::operator<<(int)' thread_test.cpp:(.text+0x7a): undefined reference tostd::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 to
std::ostream::operator<<(std::ostream& (*)(std::ostream&))' thread_test.cpp:(.text+0xcc): undefined reference tostd::cout' thread_test.cpp:(.text+0xd1): undefined reference to
std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)' thread_test.cpp:(.text+0xde): undefined reference tostd::ostream::operator<<(int)' thread_test.cpp:(.text+0xe3): undefined reference to
std::basic_ostream >& std::endl >(std::basic_ostream >&)' thread_test.cpp:(.text+0xeb): undefined reference tostd::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function
__static_initialization_and_destruction_0(int, int)': thread_test.cpp:(.text+0x141): undefined reference tostd::ios_base::Init::Init()' thread_test.cpp:(.text+0x150): undefined reference to
std::ios_base::Init::~Init()' /tmp/ccmpQLyp.o:(.eh_frame+0x47): undefined reference to `__gxx_personality_v0' collect2: error: ld returned 1 exit status
你能幫忙嗎?我需要做些什麼才能讓代碼在Linux和Windows上運行?
嘗試-lpthread,而不是 – SChepurin 2013-04-23 11:57:18
使用GCC與-lpthread我仍然得到錯誤:/tmp/ccq3Kk7G.o:在函數'主 ' thread_test.cpp :(文字+ 0xB9)值:未定義的引用'在pthread_create' collect2:error:ld返回1退出狀態 juliano @ juliano-linux:〜/ Documents/cpp $ gcc -lpthread thread_test.cpp /tmp/ccVu4YcA.o:在函數'PrintHello(void *)'中: thread_test.cpp :(.text + 0x1a):未定義的引用'std :: cout' thread_test.cpp :(。text + 0x1f):未定義的引用std :: basic_ostream>&std ::運算符<< >(std :: basic_ostream >&,char const *)' –
2013-04-23 12:02:43
gcc -o thread_test.c -Wall -Werror -lpthread – SChepurin 2013-04-23 12:19:03