Hello! There is a project, the structure of which is approximately the same (further code on c, below on c ++):

ah

#ifndef A_H #define A_H #include "bh" #include "ch" struct a_t { char *f1; char *f2; int f3; int f4; // ... }; typedef struct a_t a; a generate_a() { a ret; make_b_in_a(&ret); make_c_in_a(&ret); // ... return ret; } void print_a(a to_print) {/* ... */} #endif 

bh

 #ifndef B_H #define B_H #include "ah" struct b_t { int f1; int f2; int f3; // ... }; typedef struct b_t B; void make_b_in_a(a *obj) {/* ... */} #endif 

ch

 #ifndef C_H #define C_H #include "ah" struct c_t {/* ... */}; typedef struct c_t c; void make_c_in_a(a *obj) {/* ... */} #endif 

ch

 #include "ah" int main() { a test = generate_a(); return 0; } 

When trying to compile these four files:

 gcc main.c -o main.exe 

I get errors:

 In file included from ah:4:0, from main.c:1: bh:17:18: error: unknown type name 'a' void make_b_in_a(a *obj) ^ In file included from ah:5:0, from main.c:1: ch:11:18: error: unknown type name 'a' void make_c_in_a(a *obj) ^ In file included from main.c:1:0: ah: In function 'generate_a': ah:23:2: warning: implicit declaration of function 'make_b_in_a' [-Wimplicit-fu nction-declaration] make_b_in_a(&ret); ^ ah:24:2: warning: implicit declaration of function 'make_c_in_a' [-Wimplicit-fu nction-declaration] make_c_in_a(&ret); ^ 

I thought that typedef could work within a file, but replacing a with a_t did not help.

How to make such a project compile?

PS: warning your questions: implementations of all functions are in separate .c files, I compile this way: gcc *.c -o main.exe .


Checked whether such a construction would work in C ++.

ah

 #ifndef A_H #define A_H #include "bh" class a { public: b *f; a() { f = new b(); } }; #endif 

bh

 #ifndef B_H #define B_H #include "ah" class b { public: a *f; b() { f = new a(); } }; #endif 

main.cpp

 #include "ah" int main() { a test(); return 0; } 

I get the error:

  a *f; ^ bh: In constructor 'b::b()': bh:13:3: error: 'f' was not declared in this scope f = new a(); ^ bh:13:11: error: expected type-specifier before 'a' f = new a(); 

And this, in fact, is understandable, since if we compile with the -E key, we will get such a file (too much is removed):

 class b { public: a *f; b() { f = new a(); } }; class a { public: b *f; a() { f = new b(); } }; int main() { a test(); return 0; } 

Probably, you need to somehow define a for b . How to do this in c ++, and preferably in c.

    1 answer 1

    Corrected bh

     #ifndef B_H #define B_H #include "ah" typedef struct a_t a; struct b_t { int f1; int f2; int f3; // ... }; typedef struct b_t B; void make_b_in_a(a *obj) {/* ... */} #endif 
    • 2
      In C, there is no using. - arrowd
    • I did not notice that this is a simple C - Wanket