Keeping track of #includes?

Started by
3 comments, last by Labrasones 12 years, 1 month ago
So I've started writing my own project in C++. I was getting along quite well I thought, until I started to get compile errors with multiples of the same header files trying to be compiled. I took a closer look at my #include statements and came to a terrifying realization that it was a mess. A HUGE mess. I was amazed it had taken me that long to run into that error.

My question is; how do you keep track of your includes? What method works best? (eg. flow chart, string and bulletin board, white board, just a plain list....)

Also, what is the best way to create the hierarchy of includes, for example:

Lets say I have these files.
Bone, Mesh, Model, Entity, Manager, Updater, Main

Model relies on Mesh and Bone
Entity relies on Model
Manager relies on Mesh
Updater relies on Entity
Main relies on Manager, Updater.

What is the best way to include these files according to their individual dependencies?

I get stuck like this:
Model:
#include "Bone"
#include "Mesh"

Entity:
#include "Model"

Manager:
#include "Mesh"

Updater:
#include "Entity"

What now? What do I include in Main? I need both Updater and Manager, but if I include both, I've included Mesh twice!?

I've got no strategy to contend with this. Any suggestions?
Advertisement
Have you tried something like include guards?
http://www.cplusplus.com/forum/articles/10627/
http://www.learncpp.com/cpp-tutorial/19-header-files/
There is also Organizing code files in C and C++.
Also, forward declarations can help you remove #include directives that aren't needed.
e.g.// #include "render/mesh.h" //instead of this, use a:
class Mesh; // forward declaration - says "class Mesh exists, but I don't know what it is, yet"
class Model
{
private:
Mesh* m_mesh;
};
Thanks for the suggestions! I think Hodgman's suggestion is exactly what I was looking for.
Make sure your headers have fallowing defines:

#ifndef _HEADER_NAME_
#define _HEADER_NAME_

code

#endif


this will make sure that the content of the header is processed only once

This topic is closed to new replies.

Advertisement