c编程中main函数中使用了库文件中定义的函数,编译时却提示该函数未定义,这是啥问题,坐等高手解答

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c编程中main函数中使用了库文件中定义的函数,编译时却提示该函数未定义,这是啥问题,坐等高手解答相关的知识,希望对你有一定的参考价值。

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;
求高手解答

参考技术A 原因:
头文件 pthread.h 没有包含到源文件中
解决方法:
1)将 pthread.h 复制到源文件相同的文件夹中
2)修改为:#include "pthread.h"追问

我这样尝试了,但还是不行

追答

也许pthread.h 文件本身有问题了

参考技术B 添加编译选项-lpthread,因为pthread不是标准的编译链接的库,需要自己添加 参考技术C 你编译 a.c 怎么涉及到 cco2MhOv.o 或 cco2MhOv.c ?

头文件除了 #include <pthread.h>
是否还要 #include <sys/types.h>本回答被提问者和网友采纳

Keil中C语言调用汇编函数

AT89C52 用 DS18B20 测量温度 因为C52向DS18B20读取和写入命令需要精确到1毫秒的延时 所以都是用的汇编 但C52还有其他的复杂功能比如用到了BP神经网络算法 用汇编比较麻烦 只能用C语言
怎么在Advance.c 中调用 Basic.asm 的函数 WRITE READ RESET等
回复1楼 如果我用你的方法 比如_WRITE函数
char _WRITE(void)

#pragma ASM
; Assembler Code Here
#pragma ENDASM


那么我在main函数中调用这个函数 编译器是用inline方式 还是CALL
也就是 会不会自动生成代码 消耗额外的16us周期

回复2楼 大虾真是可遇而不可求啊

参考技术A 1、在 C 文件中要嵌入汇编代码片以如下方式加入汇编代码:
#pragma ASM
; Assembler Code Here
#pragma ENDASM

2、在 Project 窗口中包含汇编代码的 C 文件上右键,选择“Options for ...”,点击右边的“Generate Assembler SRC File”
和“Assemble SRC File”,使检查框由灰色变成黑色(有效)状态;

3、根据选择的编译模式,把相应的库文件(如 Small 模式时,是 Keil\C51\Lib\C51S.Lib)加入工程中, 该文件必须作为工程的最
后文件;
参考技术B 那个人是在网上复制的,他自己也不清楚

以上是关于c编程中main函数中使用了库文件中定义的函数,编译时却提示该函数未定义,这是啥问题,坐等高手解答的主要内容,如果未能解决你的问题,请参考以下文章

C语言中在一个函数中可以声明另一个函数吗(不是定义)?在main里面声明也可以吗?

红帽linux中C语言编程如何调用数学函数

C语言中运行中,main函数被重复定义,后面的就不能运行了怎么办,下午考试,求高手

Keil中C语言调用汇编函数

编程中系统允许对库函数重新定义吗?

c语言怎么出表格