[求助]Linux下的一个线程函数(pthread_create)问题
写了个简单的创建线程的程序#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void test1(int *num);
void test2(int *num);
void cleanup(int num1,int num2);
int g1 = 0;
int g2 = 0;
int main(int args,char *arg[])
{
pthread_t thrd1,thrd2;
int ret;
ret = pthread_create(&thrd1,NULL,(void *)test1,(void *)&g1);
if(ret)
{
perror("error 1");
exit(EXIT_FAILURE);
}
ret = pthread_create(&thrd2,NULL,(void *)test2,(void *)&g2);
if(ret)
{
perror("error 2");
exit(EXIT_FAILURE);
}
pthread_join(thrd1,NULL);
pthread_join(thrd2,NULL);
cleanup(g1,g2);
exit(EXIT_SUCCESS);
}
void test1(int *num)
{
while(*num<10)
{
printf("thrd1 counter : %d\n",*num);
(*num)++;
}
}
void test2(int *num)
{
while(*num<10)
{
printf("thrd2 counter : %d\n",*num);
(*num)++;
}
}
void cleanup(int num1,int num2)
{
printf("the result is :%d\n",num1+num2);
}
编译是出现的错误
[root@localhost thread]# gcc threadcreat.c -o threadcreat
/tmp/ccqKpQ9O.o: In function `main':
threadcreat.c:(.text+0x31): undefined reference to `pthread_create'
threadcreat.c:(.text+0x76): undefined reference to `pthread_create'
threadcreat.c:(.text+0xaa): undefined reference to `pthread_join'
threadcreat.c:(.text+0xbd): undefined reference to `pthread_join'
collect2: ld 返回 1
这是什么意思?
我去/tmp 下看了没有ccqKpQ9O.o 文件
是不是因为这个原因啊!!
----------------解决方案--------------------------------------------------------
编译的时候
gcc threadcreat.c -o threadcreat -lpthread
----------------解决方案--------------------------------------------------------