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
includes debug.h
, debug.h
will try to include config.h
but since the CONFIG
guard macro will already have been defined, the retroactive "include" of config.h
will get skipped and the parsing will continue on the next line:
void somePrintingFunction(Config* conf);
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:
struct Config_t;
void somePrintingFunction(struct Config_t* conf);
Since C11, you can also do the typedef
since C11 can handle duplicated typedefs as long as they refer to the same type.
typedef struct Config_t Config;
void somePrintingFunction(Config* conf);
(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.)