Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 包括彼此中的头文件_C_Header_Include - Fatal编程技术网

C 包括彼此中的头文件

C 包括彼此中的头文件,c,header,include,C,Header,Include,如果我有两个头文件 a.h和b.h #ifndef B_H #define B_H #include "a.h" #endif 我能在b.h中加上“a.h”吗 还可以在“a.h”中加入“b.h”?不,这是行不通的。预处理器只是将您的#include“xyz.h”替换为实际文件,因此这将以无休止的递归结束。可以,但这不是一个好主意。如果确实必须这样做,可以使用include-guard防止递归(不管怎样,这都是个好主意) 在a.h中: #ifndef A_H #defin

如果我有两个头文件

a.h
b.h

#ifndef B_H
#define B_H

#include "a.h"

#endif
我能在b.h中加上“a.h”吗


还可以在“a.h”中加入“b.h”?

不,这是行不通的。预处理器只是将您的
#include“xyz.h”
替换为实际文件,因此这将以无休止的递归结束。

可以,但这不是一个好主意。如果确实必须这样做,可以使用include-guard防止递归(不管怎样,这都是个好主意)

a.h
中:

#ifndef A_H
#define A_H

#include "b.h"

#endif
b.h

#ifndef B_H
#define B_H

#include "a.h"

#endif

通常最好只在一个方向上具有依赖关系。您可以尝试添加一次
#pragma
,如果头文件像通常那样受到保护,则不会。真正的问题是使C编译器看到的依赖项在两个方向上都工作。