views:

62

answers:

2

I'm getting different errors each time I compile my C project. There are 3 sets of them that randomly appear during the building process, and all of them are

syntax error before 'foo'

or

syntax error before 'foo' token.

They happen in different files and there are no obvious syntax errors there.

I'm using Windows 7, Eclipse with the C/C++ plugin to make a C project and gcc 3.4.5 (mingw- vista special r3). The following flags are being used:

-O0 -g3 -Wall -c -fmessage-length=0  

I've checked and rechecked all my files for preprocessor syntax errors, but found nothing.

What kind of mistakes in my source code can be generating this strange behavior? How can I avoid this kind of headaches in the future?

+1  A: 

Although it's basically impossible to diagnose compilation errors without the source code, I'll give it a shot.

Seemingly unexplainable syntax errors can be caused e.g. by missing or superfluous brackets or semicolons. At least that's almost always been what I found when nothing seemed wrong with the code that the compiler complained about. So, don't just look at the same line of code, also look at the previous lines of code.

Another possible reason I can think of is that your source code makes use of non-standard C features or syntax. Or your source code makes use of C99 features while your compiler assumes C89 syntax.

stakx
Sorry for not providing source, but the errors are really scattered through the project and I'd have to link it all. I also have checked the syntax of all files that have been marked as wrong.
BoppreH
A: 

I found the error, it was a mutual "include" statement.

Client.h included Cashier.h which also included Client.h...

Apparently this can generate syntax errors at completely unrelated lines (or even files!).

BoppreH
This is why you need to use include guards. You also need to try not to do that, as this can in principle cause a stack overflow in the C preprocessor.
RBerteig