I have code with circular dependency.
old a.h file:
#ifndef A_H
#define A_H
#include "b.h"
typedef struct {
b_t *test;
} a_t;
#endif
old b.h file:
#ifndef B_H
#define B_H
#include "a.h"
typedef struct {
a_t *test;
} b_t;
#endif
I’ve just wanted to know if my solution is “proper way” to solve that problem. I want produce nice and clear code.
new a.h file:
#ifndef A_H
#define A_H
#include "b.h"
typedef struct b_t b_t;
struct a_t {
b_t *test;
};
#endif
new b.h file:
#ifndef B_H
#define B_H
#include "a.h"
typedef struct a_t a_t;
struct b_t {
a_t *test;
};
#endif
A problem with your approach is that the
typedeffora_tis in theb.h, and vice versa.A somewhat cleaner way would be to keep your
typedefwith the struct, and use structure tags in the declarations, like this:a.h
b.h