#include

Recommended Posts

Guys, does any of you know any tool that could help me to remove unneeded "#include" lines from my C++ code. You know how same file can get included in many places more than once. Thanks!

Share on other sites
I'm not sure myself, so this may be wrong!

#pragma once(...)

or

#ifndef GAMECLASS_H#define GAMECLASS_H//Code#endif

Hope this helps a little... [smile]

Share on other sites
As far as header file sentries goes...

Quote:
 Original post by Samsonite#pragma once

Those work well if supported by the compiler. Most of them do these days.

Quote:

These are the standard way of doing it -- and it works with all compilers where the pragma does not.

You really need to use better names, though. A common problem with larger projects is that you will encounter headers with the same name in different directories, so their names will clash.

You should add something that is moderaetly unique, such as the date, an auto-generated guid, your name + date + time, or whatever.

Share on other sites
Quote:
 Original post by Niko1DudeGuys,does any of you know any tool that could help me to remove unneeded "#include" lines from my C++ code. You know how same file can get included in many places more than once.Thanks!

Do you mean that you (or another programmer) littered an implementation file with a bunch of extra #includes that are not needed?

example:
#include "networking.h"
#include "graphics.h"
#include "scenegraph.h"
#include "kitchensink.h"

even though the file itself needs none of those?

The process of elimination works well. Comment one out (or more if you want) and recompile. If you can build without it, then it wasn't needed. Repeat until they are all either commented out or required for something.

Share on other sites
Thanks!

I am already doing it and everything works fine, but I am looking for somthing different:

What I am doing is trying to clean my code an trying to avoid scenario like this or similar.

file B_FILE.H includes file A_FILE.H
file C_FILE.H includes file A_FILE.H and file B_FILE.H

etc

and it can get realy tricky.

I am afraid that once my software gets big (and it's already big) this sloppiness can introduce problems like circular dependency.

Share on other sites
Elimination process would work, but it would take forever for alll the code I got :)

Share on other sites
Quote:
 Original post by Niko1DudeI am afraid that once my software gets big (and it's already big) this sloppiness can introduce problems like circular dependency.

There are no good tools to do that.

The collected wizdom is that you make you header files idempotent. That means order doesn't matter and they can be included multiple times without harm

The only potable way to do that is the use of include guards (ifndef/define/endif). With that, you cannot get circulkar dependencies and all the headaches go away. It's just not really worth doing anything else, in my experience.

Share on other sites
Besides manually removing one include after another, it sShouldn't be too
hard to automagically remove redundant #include's from the .cpp
(recursively loop through all files, load file to memory remove one
#include-line, save to file, use the commandline compiler to compile,
check return code to see if it compiled)...

Unfortunately that won't work to remove #include's from .h files,
since other files depend on that.
Furthermore to be really effective, an 'include-killer-app' would
have to be able to do forward declarations...

Unfortunately - as far as I know - there is no program like that..

If someone else knows an app which can handle it, please enlighten us :)

/R

Share on other sites
Quote:
 Original post by frobexample:#include "networking.h"#include "graphics.h"#include "scenegraph.h"#include "kitchensink.h"even though the file itself needs none of those?The process of elimination works well. Comment one out (or more if you want) and recompile. If you can build without it, then it wasn't needed. Repeat until they are all either commented out or required for something.

This isn't the best... For example, say you have file.cpp, which uses a "cout", so you include "iostream". Now, by process of elimination, you comment out "#include <iostream>", and it still works, b/c you included <iostream> in "kitchensink.h" for example, and "kitchensink.h" is included in "file.cpp".

Now, what if you decide to remove the kitchen sink function calls, or even the kitchen sink class altogether? Then, logically, you remove "#include "kitchensink.h"". Now, it complains about missing "iostream".

Not that you couldn't just add "#include <iostream>" at that point... but the point is, the "#include <iostream>" was only in one place, but used in many.

IMO: Include headers everywhere you need them, and use include guards. Anyone do this? Or does everyone use a global include file (like pre-compiled headers) for headers like <iostream>?

Share on other sites
Quote:
 Original post by discman1028IMO: Include headers everywhere you need them, and use include guards. Anyone do this?

Yes (assuming you mean "directly include what you directly use, rather than expecting to have it included indirectly; but don't include on behalf of other things you include, because they have already included what *they* need").

Share on other sites
here's how i do it. i make a header called main.h that includes all other headers.

// main.h#ifndef _MAIN_H_#define _MAIN_H_// common headers#include <windows.h>#include "stuff.h"// classesclass CMyClass;// class headers#include "myclass.h"#endif//end

i always put class prototypes before class headers so that there is no conflict with unrecognized classes or anything like that. all other files can include main.h so i never have to worry about including the right headers in the right sources and what not. and with a main header like this, none of the other headers need #pragma once or anything like that, cause they will all be included once automatically. i don't know if anybody else does it this way but it has never given me a problem.

Share on other sites
That's one way to do it... beware though for larger projects

I did more or less the same thing you suggested above, including dx in one
of the base header files, which in turn included more or less entire windows,
stl and I don't know what.

This method does affect compile times. In your average 50k line project it
probably won't matter so much, but in one of the core libraries I removed
the global includes and moved them only to the places where they were really
needed (using forward declarations in header files, to move as much as possible
into the .cpp files).
Compile times decreased from 3:40 to 2:40 - that's 1 minute saved for
everytime someone on the project does a rebuild (and it can be optimized
even more, I only partially finished the change!)

In msvc7 under your project settings, c++ advanced (if I remember correctly), you can turn on "show include files" and then compile one file - it'll show
you everything which is included (which like I said in the case of dx is
scaringly long).

[Edited by - Kitt3n on March 17, 2006 7:10:11 PM]

Create an account

Register a new account

• Forum Statistics

• Total Topics
628400
• Total Posts
2982447

• 10
• 9
• 19
• 24
• 10