当前位置: 技术问答>linux和unix
一个多线程的程序为何调不通?
来源: 互联网 发布时间:2015-09-19
本文导语: #include #include #include #include #include #include #include #include #include #include #include void Display_information(void *ptr); main() { pthread_t thread1,thread2; char *information1="Hello"; char *information2="World"; int i; if(pthread_create(&threa...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void Display_information(void *ptr);
main()
{
pthread_t thread1,thread2;
char *information1="Hello";
char *information2="World";
int i;
if(pthread_create(&thread1,NULL,(void*)&Display_information,(void*)information1)!=0)
{
fprintf(stderr,"Create Thread[%d]Error:%sna",i,strerror(errno));
exit(1);
}
sleep(10);
if(pthread_create(&thread2,NULL,(void*)&Display_information,(void*)information2)!=0)
{
fprintf(stderr,"Create Thread[%d]Error:%sna",i,strerror(errno));
exit(1);
}
sleep(10);
exit(0);
}
void Display_information(void*ptr)
{
char *information;
information=(char*)ptr;
printf("%s",information);
pthread_exit(0);
}
出现以下调式信息:
: undefined reference to `pthread_create'
我就想问大家pthread_create是系统函数,怎么还要定义!?
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void Display_information(void *ptr);
main()
{
pthread_t thread1,thread2;
char *information1="Hello";
char *information2="World";
int i;
if(pthread_create(&thread1,NULL,(void*)&Display_information,(void*)information1)!=0)
{
fprintf(stderr,"Create Thread[%d]Error:%sna",i,strerror(errno));
exit(1);
}
sleep(10);
if(pthread_create(&thread2,NULL,(void*)&Display_information,(void*)information2)!=0)
{
fprintf(stderr,"Create Thread[%d]Error:%sna",i,strerror(errno));
exit(1);
}
sleep(10);
exit(0);
}
void Display_information(void*ptr)
{
char *information;
information=(char*)ptr;
printf("%s",information);
pthread_exit(0);
}
出现以下调式信息:
: undefined reference to `pthread_create'
我就想问大家pthread_create是系统函数,怎么还要定义!?
|
gcc thread.cpp -lpthread -o thread
编译时加上 -lpthread
编译时加上 -lpthread
|
reference to `pthread_create'
是提示没有找到pthead_create的link
需要加上-lpthread
是提示没有找到pthead_create的link
需要加上-lpthread