tags:

views:

732

answers:

5

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.

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

Contents of a.h:

#ifndef __A_H
#define __A_H

#include "b.h"

#endif // __A_H

Contents of b.h:

#ifndef __B_H
#define __B_H

#include "c.h"

#endif // __B_H

Contents of c.h:

#ifndef __C_H
#define __C_H

#include "a.h"

#endif // __C_H

But it doesn't seem to work.

+5  A: 

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.

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:

gcc -E $(CFLAGS) -o foo.i foo.cpp
florin
I believe you're right. I think my problem had to do with an enum being defined in a header file.
MSumulong
+1  A: 

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

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

gnud
Macros with leading underscores, or double underscores anywhere (C++ only, I think). There are several items on stackoverlfow that discuss the forms of reserved names in C/C++
Michael Burr
@gnud: Avoid double-underscore (for C++ compatibility) and (slightly simplistically) leading underscores are reserved to the implementation - as Mike B said. You may use single under-scores everywhere else.
Jonathan Leffler
Yes, I meant leading underscores. Editing to fix now.
gnud
+1  A: 

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.

tgamblin
A: 

This works.

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

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

Michael Burr
+1  A: 

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

Manoj Doubts