首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

undefined reference to 'pthread_create',该怎么解决

2012-11-05 
undefined reference to pthread_create我在linux写下面c程序时,总是报错,说undefined reference to pt

undefined reference to 'pthread_create'
我在linux写下面c程序时,总是报错,说
undefined reference to 'pthread_create'
undefined reference to 'pthread_join'

但是我看见pthread.h头文件下面有这个函数的定义啊。这是怎么回事呢?



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define NLOOP 5000
int counter;
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;

void *doit(void *vptr)
{
int i,val;
for(i=0;i<NLOOP;i++)
{
pthread_mutex_lock(&counter_mutex);
val = counter;
printf("%x: %d\n",(unsigned int)pthread_self(),val+1);
counter+=val+1;
pthread_mutex_unlock(&counter_mutex);
}
return NULL;
}

int main(void) {
pthread_t tidA,tidB;
pthread_create(&tidA,NULL,doit,NULL);
pthread_create(&tidB,NULL,doit,NULL);
pthread_join(tidA,NULL);
pthread_join(tidB,NULL);
return EXIT_SUCCESS;
}


[解决办法]
编译的时候加-pthread

热点排行