尝试在Ubuntu上使用pthread时出错
我正在阅读关于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 to
std :: cout'thread_test.cpp :( .text + 0x1f):undefined引用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 to
std :: 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 to
标准:: ostream的::的operator <<(标准:: ostream的&(*)(标准:: ostream的&))” /tmp/ccmpQLyp.o:在功能main': thread_test.cpp:(.text+0x63): undefined reference to
std :: cout'thread_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 to
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 to
std :: ostream :: operator <<(int)'thread_test.cpp :(。text + 0x7a): 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 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 to
std :: ostream :: operator <<(std :: ostream&(*)(std :: ostream&))'thread_test.cpp :(。text + (std :: basic_ostream>&,char const *):未定义的引用std::cout' thread_test.cpp:(.text+0xd1): undefined reference to
std :: basic_ostream>&std :: operator <<> 'thread_test.cpp :(。text + 0xde):未定义的引用std::ostream::operator<<(int)' thread_test.cpp:(.text+0xe3): undefined reference to
std :: basic_ostream>&std: std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function
:endl>(std :: basic_ostream>&)'thread_test.cpp :(。text + 0xeb):未定义的引用std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function
__static_initialization_and_destruction_0 std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function
(int,int)':thread_test.cpp :(。text + 0x141):对std::ios_base::Init::Init()' thread_test.cpp:(.text+0x150): undefined reference to
std: :ios_base :: Init ::〜Init()'/tmp/ccmpQLyp.o:(.eh_frame+0x47):未定义引用'__gxx_personality_v0'collect2:错误:ld返回1退出状态
你能帮我吗? 我需要做些什么才能让代码在Linux和Windows上运行?
使用g++
而不是gcc
,或手动链接-lstdc++
。