C头文件中的包含周期

时间:2022-04-04 11:45:58

How does one prevent an inclusion cycle in C? ie. You shouldn't have a.h #include "b.h", which #include's "c.h" which #include's "a.h". I'm looking for a way of preventing this from happening using some sort of C directive.

如何防止C中的包含循环?即。你不应该有一个#include“b.h”,其中#include的“c.h”是#include的“a.h”。我正在寻找一种使用某种C指令防止这种情况发生的方法。

I had originally thought this would've prevented this from happening:

我本来以为这会阻止这种情况发生:

Contents of a.h:

a.h的内容:

#ifndef __A_H
#define __A_H

#include "b.h"

#endif // __A_H

Contents of b.h:

b.h的内容:

#ifndef __B_H
#define __B_H

#include "c.h"

#endif // __B_H

Contents of c.h:

c.h的内容:

#ifndef __C_H
#define __C_H

#include "a.h"

#endif // __C_H

But it doesn't seem to work.

但它似乎没有用。

5 个解决方案

#1


7  

It does work allright: the files are repeatedly included, but the sections protected by #ifdndef/#define/#endif are not repeated, and that breaks the cycle.

它确实工作正常:文件被重复包含,但#ifdndef / #define / #endif保护的部分不会重复,这会打破循环。

Use your compiler to produce the preprocessed output and look at it for yourself. With GNU CC, you need to use "-E" option on the .c[pp] file, like this:

使用编译器生成预处理输出并自行查看。使用GNU CC,您需要在.c [pp]文件中使用“-E”选项,如下所示:

gcc -E $(CFLAGS) -o foo.i foo.cpp

#2


1  

That should work. It's written correctly in your example and compiles fine for me. Did you mistype something in your actual code, or is it really some other problem you're seeing?

这应该工作。它在您的示例中正确编写并为我编译好。您是否在实际代码中输入了错误的内容,或者您​​是否真的遇到了其他问题?

You shouldn't start things out with __, though, as that's reserved for the compiler and/or system libraries. Try some other names for your guards.

但是,您不应该使用__开始,因为它是为编译器和/或系统库保留的。为你的警卫尝试一些其他的名字。

#3


1  

Macros with leading underscores are reserved for the preprocessor/compiler.

带有前导下划线的宏保留给预处理器/编译器。

Try changing __*_H to something more standard.
I use HAVE__*_H.

尝试将__ * _ H更改为更标准的内容。我用HAVE __ * _ H。

#4


1  

ya in addition to the above things if you are working on turbo c and you are doing a project with these source files then do not attach the header files which are #included in the source files.And even then if it is not working then try it from command prompt because some compiler options give these errors again and again.so here if the header files contents are between the #ifndef and #endif then there will be no problem even you include both the files. So try removing the header files from the project keeping them in the same directory.bcos u didnt specified environment i specified turbo C because i faced this situation once on turbo C with the header files #included in source file and attached to the project files list then there will be "multiple declaration problem".also after compiling (even with errors) go to external command line and go to directory where that file is stored and try with the filename.exe directly.ok

ya除了上面的内容,如果你正在使用turbo c并且你正在用这些源文件做一个项目,那么就不要在源文件中附加#included的头文件。即使那样,如果它不工作那么试试它来自命令提示符,因为一些编译器选项一次又一次地给出这些错误。如果头文件内容在#ifndef和#endif之间,那么即使你包含这两个文件也没有问题。所以尝试从项目中删除头文件,将它们保存在同一个目录中.bcos你没有指定环境我指定turbo C因为我在turbo C上面对这种情况,头文件#included在源文件中并附加到项目文件列表然后会出现“多重声明问题”。编译后(即使有错误)也可以转到外部命令行,然后转到存放该文件的目录,直接尝试使用filename.exe。

#5


0  

This works.

Just to be sure, I actually compiled a test.c that included a.h with your 3 header files.

为了确定,我实际上编译了一个test.c,其中包含带有3个头文件的a.h.

I verified this works for several versions of MSVC, Digital Mars and GCC.

我验证了这适用于MSVC,Digital Mars和GCC的几个版本。

#1


7  

It does work allright: the files are repeatedly included, but the sections protected by #ifdndef/#define/#endif are not repeated, and that breaks the cycle.

它确实工作正常:文件被重复包含,但#ifdndef / #define / #endif保护的部分不会重复,这会打破循环。

Use your compiler to produce the preprocessed output and look at it for yourself. With GNU CC, you need to use "-E" option on the .c[pp] file, like this:

使用编译器生成预处理输出并自行查看。使用GNU CC,您需要在.c [pp]文件中使用“-E”选项,如下所示:

gcc -E $(CFLAGS) -o foo.i foo.cpp

#2


1  

That should work. It's written correctly in your example and compiles fine for me. Did you mistype something in your actual code, or is it really some other problem you're seeing?

这应该工作。它在您的示例中正确编写并为我编译好。您是否在实际代码中输入了错误的内容,或者您​​是否真的遇到了其他问题?

You shouldn't start things out with __, though, as that's reserved for the compiler and/or system libraries. Try some other names for your guards.

但是,您不应该使用__开始,因为它是为编译器和/或系统库保留的。为你的警卫尝试一些其他的名字。

#3


1  

Macros with leading underscores are reserved for the preprocessor/compiler.

带有前导下划线的宏保留给预处理器/编译器。

Try changing __*_H to something more standard.
I use HAVE__*_H.

尝试将__ * _ H更改为更标准的内容。我用HAVE __ * _ H。

#4


1  

ya in addition to the above things if you are working on turbo c and you are doing a project with these source files then do not attach the header files which are #included in the source files.And even then if it is not working then try it from command prompt because some compiler options give these errors again and again.so here if the header files contents are between the #ifndef and #endif then there will be no problem even you include both the files. So try removing the header files from the project keeping them in the same directory.bcos u didnt specified environment i specified turbo C because i faced this situation once on turbo C with the header files #included in source file and attached to the project files list then there will be "multiple declaration problem".also after compiling (even with errors) go to external command line and go to directory where that file is stored and try with the filename.exe directly.ok

ya除了上面的内容,如果你正在使用turbo c并且你正在用这些源文件做一个项目,那么就不要在源文件中附加#included的头文件。即使那样,如果它不工作那么试试它来自命令提示符,因为一些编译器选项一次又一次地给出这些错误。如果头文件内容在#ifndef和#endif之间,那么即使你包含这两个文件也没有问题。所以尝试从项目中删除头文件,将它们保存在同一个目录中.bcos你没有指定环境我指定turbo C因为我在turbo C上面对这种情况,头文件#included在源文件中并附加到项目文件列表然后会出现“多重声明问题”。编译后(即使有错误)也可以转到外部命令行,然后转到存放该文件的目录,直接尝试使用filename.exe。

#5


0  

This works.

Just to be sure, I actually compiled a test.c that included a.h with your 3 header files.

为了确定,我实际上编译了一个test.c,其中包含带有3个头文件的a.h.

I verified this works for several versions of MSVC, Digital Mars and GCC.

我验证了这适用于MSVC,Digital Mars和GCC的几个版本。