Jump to content
  • Advertisement
Sign in to follow this  
Niko1Dude

#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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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>?

Share this post


Link to post
Share on other sites
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").

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!