views:

143

answers:

2

Are there any tools that help organizing the #includes that belong at the top of a .c or .h file?

I was just wondering because I am reorganizing my code, moving various small function definitions/declarations from one long file into different smaller files. Now each of the smaller files needs a subset of the #includes that were at the top of the long file.

It's just annoying and error-prone to figure out all #includes by hand. Often the code compiles even though not all #includes are there. Example: File A uses std::vector extensively but doesn't include vector; but it currently includes some obscure other header which happens to include vector (maybe through some recursive includes).

+1  A: 

makedepend and gccmakedep

msw
Good idea, but probably not exactly what I need. I'd like a tool that tells me what I should include, even if I haven't added a single include yet (makedepend and friends would give an error in that case). Eclipse does that for Java (maybe for C++ too?). Also, makedepend tells me the dependencies for each compilation unit (`.o` file), but not what a certain header file should include.
I think the problem as you pose it is uninvertable. For example, I can run a program that tells me that `EOF` is undefined in `foo.c`. I can also mechanically determine that EOF is defined in `libio.h`, `stdio.h` and (e.g.) `foo.h`. Which do I include?
msw
@msw, you present a valid point. However, a tool could still make suggestions, based on which headers are present in the include path, and perhaps even which are standard (or popular).
Matthew Flaschen
It would also be useful to know what header files that are included have no effect on the compilation. Certainly, one could drop one header at a time and compile, but there must be an easier way. That would catch the common case of a header that was required in an earlier revision, but which is now unnecessary.
RBerteig
+1  A: 

VisualAssistX can help you jumping to the definition of a type. E.g. if you use a class MyClass in your source, you can click it, choose goto definition, and VisualAssistX opens the include file that contains the definition of this class (possibly Visual Studio can also do this, but at this point am so getting used to VisualAssistX, that I contribute every wonderful feature to VisualAssistX :-)). You can use this to find the include file necessary for your source code.

PC-Lint can do exactly the opposite. If you have an include file in your source that is not being used, PC-Lint can warn you about it, so that you know that the include file can be removed from the source (which will have a positive impact on your compilation time).

Patrick