分享

C语言头文件相互包含的问题

 beginnow1 2022-07-29 发布于广东

 我深知前路风雨,但我依然微笑前行 ;)

头文件相互包含的问题

问题:

  1. 头文件交叉包含是否会导致递归包含,导致编译出错?
  2. 如果不会因为递归包含出错,那么交叉包含是不是完全没问题?

1.1 头文件交叉包含是否会导致递归包含,导致编译出错?(无#ifndef

  假若头文件a包含了头文件b、头文件b又包含了头文件a,那么在#include头文件a的时候,就可能会导致递归包含,从而导致编译出错;

/* a.h */
#include "b.h"
#define A_H 1
/* b.h */
#include "a.h"
#define B_H 2
/* main.c */
#include <stdlib.h>
#include <stdio.h>
#include "a.h"
#include "b.h"

void main()
{
	printf("hello world!\n");
}
  • 在Visual Studio中会提示,头文件包含了自身;编译时会报错:
    • fatal error C1014: 包含文件太多 : 深度 = 1024
  • 在Ubuntu下面编译会报错:
    • a.h:1:15: error: #include nested too deeply
    • b.h:1:15: error: #include nested too deeply

1.2头文件交叉包含是否会导致递归包含,导致编译出错?(有#ifndef

  修改a.h、b.h两个文件,main.c文件内容不变。此时编译正常通过:因此可以知道,当头文件相互包含时,只要有预处理#ifndef就可以保证理论上不会出错。

/* a.h */
#ifdef __A_H_
#define __A_H

#include "b.h"
#define A_H 1

#endif
/* b.h */
#ifndef __B_H_
#define __B_H

#include "a.h"
#define B_H 2

#endif

1.3 如果不会因为递归包含出错,那么交叉包含是不是完全没问题?

  从上面的头文件内容可以看出,虽然两个头文件相互包含,但是两个头文件内容并不相互引用。因此编译不会出现问题。但是如果像下面文件,头文件变量又相互引用,则编译会出现变量未知错误

a.h:9:9: error: unknown type name 'B_H’

/* a.h */
#ifndef __A_H_
#define __A_H_
#include "b.h"

typedef struct _A_H{
	int a;
} A_H;

int fun(B_H *ptr);

#endif
/* b.h */
#ifndef __B_H_
#define __B_H_
#include "a.h"

typedef struct _B_H{
	A_H b;
} B_H;

#endif
/* main.c */
#include <stdio.h>
#include "b.h"
#define CC DD
#define DD 2

void main()
{
	printf("hello world!\n");
}

  使用 'gcc -E main.c > tmp’ 将main.c只做预处理,可以看到预处理后的文件内容为:所以是因为变量定义的顺序问题导致问题。

/* tmp 部分内容*/
# 1 "b.h" 1
# 4 "a.h" 2

typedef struct _A_H{
 int a;
} A_H;

int fun(B_H *ptr);
# 4 "b.h" 2

typedef struct _B_H{
 A_H b;
} B_H;
# 3 "main.c" 2



int main()
{
 printf("hello world!\n");
 return 0;
}

1.4 但是将b.h文件中的结构体加上struct又可以编译成功了!!!

此时会出现警告:结构体隐性申明

a.h:9:16: warning: 'struct _B_H’ declared inside parameter list [enabled by default]

/* a.h */
#ifndef __A_H_
#define __A_H_
#include "b.h"

typedef struct _A_H{
	int a;
} A_H;

int fun(struct _B_H *ptr);

#endif
/* b.h */
#ifndef __B_H_
#define __B_H_
#include "a.h"

typedef struct _B_H{
	struct _A_H b;
} B_H;

#endif

总结

  头文件相互包含,而且变量又相互引用。此时应该将其中一个头文件拆成两个头文件b1.h、b2.h,b1.h让原来的a.h包含;b2.h用来包含a.h。不要使两个头文件变量相互引用引用的设计方式出现。

从上面main.c内容可以看出宏定义的顺序可以不按顺序出现,编译也不会出错。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多