+1  A: 

I think using some editor with brace highlighting will help. There should also be some tools around that do automatic indention on code.

EDIT: Does this vim script help? It seems to do #ifdef highlighting.

schnaader
Yes, but that only helps me with the source file proper. The nasty part is that the source file is including almost a hundred header files, and one of those headers is likely where the problem exists.
flodin
See my edit, #ifdef highlighting could help with the header files
schnaader
That vim script looks really useful, thanks! Still it won't help me if the problem exists in one of the header files.
flodin
Why wouldn't it? You can edit the header files, can't you, so the highlighting works for them, too. If editing all the header files is too much work, you can comment the "#include" statements until the code compiles and find the header files that causes the problem this way.
schnaader
That is certainly a way of doing it. I wouldn't say it is too much work, but I would love it if there was a more efficient way. Keep in mind that those 100 include files in turn include other files, and the problem could be several levels deep. The total number of included files could easily be in the thousands, so it's a lot of trial and error. Add do that a lot of #ifdefs that contain nested #ifdefs and #defines; just tracking down what is defined and what isn't takes a lot of effort, but is necessary to follow preprocessor's parsing path.
flodin
+2  A: 

Hints:

  • make small changes and recompile after each small change
  • for unmatched braces, use an editor/IDE that supports brace match hilighting
  • if all else failds, Ye Olde method of commenting out blocks of code using a binary chop approach works for me
anon
A: 

Have a look at this question (Highlighting unmatched brackets in vim)

kgiannakakis
+5  A: 

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.

njplumridge
A binary search is your friend here. And you can do the same thing with #endif, not to mention spanning the rest of the source file. Its not fixed when the error stops, of course ;-)
RBerteig
+1  A: 

Can you create a script that actually does all the including, and then write the whole stuff to a temporary file? Or try the compiler for helping you with that? Then you can use the bracket highlighting features of various editors to find the problem and you'll probably be able to identify the file. (An extra help can be to have your script add a comment around every included file).

andreas buykx
A: 

Pre-compile your code first, this will create a big chunk with the include files stuffed into the same file. Then you can use those brace-matching scripts.

Makis
This works if the problem is an unmatched brace. But if it's an unmatched #ifdef, the preprocessor will refuse to run.
flodin
Well, after that you at least know which it is. If it's the preprocessor directives, you could then do something like (copy all files to the same directory first)grep -n -P \#ifdef\|#else\|#elif\|#endif * > precomp.txtAnd unless the amount of these constructs is huge, you should be able to go through that file. A couple of hundred of lines in that file wouldn't take too long to check.
Makis
+1  A: 

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.

JRL
+3  A: 

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.

Steve Jessop
Regarding the second tip: for us I think the problem is more often that people don't build the whole product before checking in. If the problem is in a header file, it probably worked for whoever checked it in, in the components he was building. But in another context, with other #defines, in a different project, the same file suddenly starts causing problems.
flodin
Still, if that other developer had checked in a bunch of intermediate versions, then at the forensics stage you could pull each of them in turn, compile it in the configuration which is now failing, and perhaps narrow down the point which caused the problem. Not guaranteed, though, since they may have taken the approach of "get my config working first, then jigger it to make the rest work". In which case their intermediate versions will fail for reasons unrelated to the reason their final version fails.
Steve Jessop