Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hints and tools for finding unmatched braces / preprocessor directives

Tags:

c++

c

This is one of my most dreaded C/C++ compiler errors:

file.cpp(3124) : fatal error C1004: unexpected end-of-file found

file.cpp includes almost a hundred header files, which in turn include other header files. It's over 3000 lines. The code should be modularized and structured, the source files smaller. We should refactor it. As a programmer there's always a wish list for improving things.

But right now, the code is a mess and the deadline is around the corner. Somewhere among all these lines—quite possibly in one of the included header files, not in the source file itself—there's apparently an unmatched brace, unmatched #ifdef or similar. The problem is that when something is missing, the compiler can't really tell me where it is missing. It just knows that when it reached end of the file it wasn't in the right parser state.

Can you offer some tools or other hints / methodologies to help me find the cause for the error?

like image 982
flodin Avatar asked May 08 '09 11:05

flodin


3 Answers

If the #includes are all in one place in the source file, you could try putting a stray closing brace in between the #includes. If you get an 'unmatched closing brace' error when you compile, you know it all balances up to that point. It's a slow method, but it might help you pinpoint the problem.

like image 117
njplumridge Avatar answered Nov 13 '22 07:11

njplumridge


One approach: if you have Notepad++, open all of the files (no problem opening 100 files), search for { and } (Search -> Find -> Find in All Open Documents), note the difference in count (should be 1). Randomly close 10 files and see if the difference in count is still 1, if so continue, else the problem is in one of those files. Repeat.

like image 41
JRL Avatar answered Nov 13 '22 06:11

JRL


Handy tip:

For each header file, auto-generate a source file which includes it, then optionally contains an empty main method, and does nothing else. Compile all of these files as test cases, although there's no point running them.

Provided that each header includes its own dependencies (which is a big "provided"), this should give you a better idea which header is causing the problem.

This tip is adapted from Google's published C++ style guide, which says that each component's source files should include the interface header for that component before any other header. This has the same effect, of ensuring that there is at least one source file which will fail to compile, and implicate that header, if there's anything wrong with it.

Of course it won't catch unmatched braces in macros, so if you use much in the way of macros, you may have to resort to running the preprocessor over your source file, and examining the result by hand and IDE.

Another handy tip, which is too late now:

Check in more often (on a private branch to keep unfinished code out of everyone else's way). That way, when things get nasty you can diff against the last thing that compiled, and focus on the changed lines.

like image 36
Steve Jessop Avatar answered Nov 13 '22 08:11

Steve Jessop