#include

Started by
10 comments, last by Kitt3n 18 years, 1 month ago
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!
Advertisement
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]

Hope I was helpful. And thank you if you were!
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.

Quote:Original post by Niko1Dude
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!


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.
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.
Elimination process would work, but it would take forever for alll the code I got :)
Quote:Original post by Niko1Dude
I 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.

Stephen M. Webb
Professional Free Software Developer

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
visit my website at www.kalmiya.com
Quote:Original post by frob
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.


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>?
--== discman1028 ==--
Quote:Original post by discman1028
IMO: 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").

This topic is closed to new replies.

Advertisement