-1

I am attempting to separate my function prototypes and the actual implementantions between .h and .c files to make it easier to see what functions I have.

my compiler, visual studio, has removed the curly green underline from under the function prototypes in the header files implying it is seeing the implementations in the .c file, but when I try to compile it I get 100+ errors all saying stuff like "syntax error: missing ';' before '*'" or "syntax error: missing ')' before '*'" or "syntax error: missing '}' before '*'" both in the .h and .c files. They are mostly pointing to the parameters in the prototypes, but I could not go trough all to know for sure.

Is there some other special way to do this?

I tried:

  • Making them extern
  • Changing guards to a name i know for sure isnt already used

I do not know what else I can try.

NiInThTrCo
  • 17
  • 4
  • 1
    Make a small example .h and .c file that won't compile and post them in your question. Surely the answer is in the code, we can't fix what we can't see. – John Zwinck Jul 11 '20 at 02:20
  • Whichever error is listed first, fix that first and recompile, then fix the next. – dbush Jul 11 '20 at 02:21
  • Thanks, but I fixed it by myself. the problem was a typedef'd struct circular dependency – NiInThTrCo Jul 11 '20 at 02:31
  • Read a good C programming book such as [*Modern C*](https://modernc.gforge.inria.fr/) and a good C++ programming book such as [*Programming in C++*](https://stroustrup.com/programming.html) – Basile Starynkevitch Jul 11 '20 at 06:44

2 Answers2

1

The first thing you could try is posting your code so we can see it :-)

However, the error syntax error: missing ';' before ... almost always shows up because you have an unknown type, as per the following:

int main() {
    NOTYPE *x;
    return 0;
}

Since you're moving function prototypes into a header, it may be that they're using types that still exist in the C file. Those type declarations must exist before use, or you will get that error.

That (function prototypes with unknown types) is not the only reason that error occurs but I can almost guarantee it'll be a similar issue, trying the use a type not yet declared.


As per other information posted here, you can also get this problem with circular dependencies, such as with:

typedef struct { TWO *two; } ONE; // don't know anything about TWO here.
typedef struct { ONE *one; } TWO;

If the dependency was only one way, then you could just ensure the order of the two definitions was correct. With a circular dependency, you have to introduce an incomplete definition (really a declaration) to allow it to work:

typedef struct TWO;                // declare that TWO exists.
typedef struct { TWO *two; } ONE;  // define ONE, we know TWO exists.
typedef struct { ONE *one; } TWO;  // define ONE, we know all about ONE.

Note that this only works if you declare a pointer to the incomplete type, you won't be able to do:

typedef struct TWO;
typedef struct { TWO two; } ONE;

because the definition of ONE requires a complete type.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

I have figured out the error.

The problem was that I had a circular dependency with 2 structures and I tried to fix it by defining them both before implementing them, but that obviously did not work.

this was my code:

typedef struct A;
typedef struct B;

typedef struct {
    B* blist;
}A;

typedef struct {
    A* a_parent;
}B;

The following stackoverflow answer helped me fix my errors:

Resolve circular typedef dependency?

NiInThTrCo
  • 17
  • 4
  • I'm guessing those two incomplete types at the top are the *fix* since, with those, the code is correct. In any case, you only need the `B` declaration, so you can successfully define `A`. The A definition/declaration already exists when you try to define `B`. – paxdiablo Jul 11 '20 at 02:49
  • I understand your reasoning, as that is the reason I tried it in the first place, but until I used that SO answer, the 100+ errors persisted, so I dont know what to tell you. – NiInThTrCo Jul 11 '20 at 03:01