c编程中main函数中使用了库文件中定义的函数,编译时却提示该函数未定义,这是什么问题,坐等高手解答
c源码如下#include<pthread.h>#include<stdio.h>#include<stdlib.h>#defineREPEAT_NUM5#defineD...
c源码如下
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define REPEAT_NUM 5
#define DELAY_TIME_LEVELS 10.0
#define THREAD_NUM 3
void * phrd_func(void *arg)
{
void * thrd_ret;
int count=0;
int delay_time=0;
int thread_num=(int)arg;
printf("thread %d is starting\n");
for(count=0;count<REPEAT_NUM;++count)
{
delay_time=(int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;
sleep(delay_time);
printf("thread %d :iob %d : delay %ds\n",thread_num,count,delay_time);
}
printf("thread %d finishde\n",thread_num);
pthread_exit(NULL);
}
int main()
{
void * thrd_ret;
pthread_t thread[THREAD_NUM];
int no,ret;
srand(time(NULL));
for(no=0;no<THREAD_NUM;++no)
{
ret=pthread_create(&thread[no],NULL,phrd_func,(void *)no);
if(ret!=0)
{
printf("error pthresd\n");
exit(ret);
}
}
printf("creat threads sucess : waiting threads finished\n");
for(no=0;no<THREAD_NUM;no++)
{
ret=pthread_join(thread[no],&thrd_ret);
if(!ret)
{
printf("thread %d joined\n",no);
}
else
{
printf("thread %d joined failed\n",no);
}
}
return 0;
}
编译报告如下
[lyc@localhost lyc]$ gcc a.c
/tmp/cco2MhOv.o(.text+0x108): In function `main':
: undefined reference to `pthread_create'
/tmp/cco2MhOv.o(.text+0x168): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
pthread.h如下:
/* Function for handling threads. */
/* Create a thread with given attributes ATTR (or default attributes
if ATTR is NULL), and call function START_ROUTINE with given
arguments ARG. */
extern int pthread_create (pthread_t *__restrict __threadp,
__const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROW;
/* Terminate calling thread. */
extern void pthread_exit (void *__retval)
__THROW __attribute__ ((__noreturn__));
/* Make calling thread wait for termination of the thread TH. The
exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
is not NULL. */
extern int pthread_join (pthread_t __th, void **__thread_return) __THROW;
求高手解答 展开
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define REPEAT_NUM 5
#define DELAY_TIME_LEVELS 10.0
#define THREAD_NUM 3
void * phrd_func(void *arg)
{
void * thrd_ret;
int count=0;
int delay_time=0;
int thread_num=(int)arg;
printf("thread %d is starting\n");
for(count=0;count<REPEAT_NUM;++count)
{
delay_time=(int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;
sleep(delay_time);
printf("thread %d :iob %d : delay %ds\n",thread_num,count,delay_time);
}
printf("thread %d finishde\n",thread_num);
pthread_exit(NULL);
}
int main()
{
void * thrd_ret;
pthread_t thread[THREAD_NUM];
int no,ret;
srand(time(NULL));
for(no=0;no<THREAD_NUM;++no)
{
ret=pthread_create(&thread[no],NULL,phrd_func,(void *)no);
if(ret!=0)
{
printf("error pthresd\n");
exit(ret);
}
}
printf("creat threads sucess : waiting threads finished\n");
for(no=0;no<THREAD_NUM;no++)
{
ret=pthread_join(thread[no],&thrd_ret);
if(!ret)
{
printf("thread %d joined\n",no);
}
else
{
printf("thread %d joined failed\n",no);
}
}
return 0;
}
编译报告如下
[lyc@localhost lyc]$ gcc a.c
/tmp/cco2MhOv.o(.text+0x108): In function `main':
: undefined reference to `pthread_create'
/tmp/cco2MhOv.o(.text+0x168): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
pthread.h如下:
/* Function for handling threads. */
/* Create a thread with given attributes ATTR (or default attributes
if ATTR is NULL), and call function START_ROUTINE with given
arguments ARG. */
extern int pthread_create (pthread_t *__restrict __threadp,
__const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROW;
/* Terminate calling thread. */
extern void pthread_exit (void *__retval)
__THROW __attribute__ ((__noreturn__));
/* Make calling thread wait for termination of the thread TH. The
exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
is not NULL. */
extern int pthread_join (pthread_t __th, void **__thread_return) __THROW;
求高手解答 展开
3个回答
展开全部
原因:
头文件 pthread.h 没有包含到源文件中
解决方法:
1)将 pthread.h 复制到源文件相同的文件夹中
2)修改为:#include "pthread.h"
头文件 pthread.h 没有包含到源文件中
解决方法:
1)将 pthread.h 复制到源文件相同的文件夹中
2)修改为:#include "pthread.h"
追问
我这样尝试了,但还是不行
追答
也许pthread.h 文件本身有问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
添加编译选项-lpthread,因为pthread不是标准的编译链接的库,需要自己添加
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询