# #include

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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:
 Original post by Samsonite#ifdef HEADER_H

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").

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 13
• 9
• 12
• 9
• ### Forum Statistics

• Total Topics
631438
• Total Posts
3000073
×