C language: recursive #include
I came across such piece of code:
a.h:
#include "b.开发者_如何学Goh"
/* structure definitions, macros etc. */
b.h:
#include "a.h"
/* structure definitions, macros etc. */
Is this legal from the C standard point of view? I would think such approach isn't safe.
You need to use include guards. Then it will be safe.
a.h
#ifndef A_H
#define A_H
/* ... */
#endif
It is legal. All compilers that I know of impose a nesting limit, usually in the range of 20 to 50. Recursion, if useful, is easily controlled with condtionals:
#if NESTING < 5
#define NESTING NESTING+1
#include "myself.h"
#endif
There are thousands of ways to shoot yourself in the foot as a programmer. This is just one more way. Be careful.
That's where include guards
come handy.
It's legal. But it's not guaranteed to result in anything useful. Use forward declarations and include guards to get rid of such circular dependencies.
精彩评论