I have 2 header files that have to include one other.
config.h:
#ifndef CONFIG
#define CONFIG
#include "debug.h"
typedef struct Config_t {
/* some stuff */
} Config;
#endif
debug.h
#ifndef DEBUG
#define DEBUG
#include "config.h"
void somePrintingFunction(Config* conf);
#endif
Here is the error I get:
debug.h: error: unknown type name 'Config'
config.c: warning: implicit declaration of function 'somePrintingFunction'
debug.h: error: unknown type name 'Config'
I guess it's looping in the header declaration?
Edit:
Fixed in merging both files so simplify project design. If you want a real fix check in comments.
When
config.h
includesdebug.h
,debug.h
will try to includeconfig.h
but since theCONFIG
guard macro will already have been defined, the retroactive "include" ofconfig.h
will get skipped and the parsing will continue on the next line:without the
Config
type having been defined.As StoryTeller has pointed out, you can break the mutual dependency by forward declaring the
Config_t
struct:Since C11, you can also do the
typedef
since C11 can handle duplicated typedefs as long as they refer to the same type.(Forward declarations of aggregates (structs or unions) don't allow you to declare objects of those types (unless a full definition has already been provided) but since C guarantees that all pointers to structs or unions must look the same, they are enough to allow you to start using pointers to those types.)