C语言,函数包含失败问题

1.头文件包含顺序出错导致头文件中的函数无法使用

eg:在主函数中调用support.h中的strcat()函数失败,但是明明已经包含了strcat()函数的头文件进来;编译器还是提示“Undefined sysbol support (refreed from xxx.o)”.

以下函数只是简单举例,请不要直接拿来编译

main中,先调用了includes.h头文件,然后再调用inside.h函数,此时编译器是先包含support.h,而没有先定义SUPPORT_EN,所以编译无法找到strcat函数。就算是第二行再包含了一次inside.h也没有用,因为编译器已经不会再包含一次support.h函数了,所以,需要先调用inside.h头文件,不可以先调用support.h头文件。

main.c

#include "includes.h"
#include "inside.h"

void main()
{
    char tmep[10] = "":
    
    strcat(temp,"hello!");
}

includes.h

#ifndef __SUPPORT_H__
#define	__SUPPORT_H__

#include "support.h"

#endif

support.h

#ifndef __SUPPORT_H__
#define	__SUPPORT_H__

#ifdef SUPPORT_EN
void strcat(char *p1,char *p2);
#endif

#endif

inside.h

#ifndef __INSIDE_H__
#define	__INSIDE_H__

#define	SUPPORT_EN
#include "support.h"

#endif

support.c

#include "inside.h"

void support(char*p1,char*p2){
    return;
}
posted @ 2023-06-13 09:58  isyf  阅读(45)  评论(0编辑  收藏  举报