Molma
Molma

Reputation: 171

How would I handle having a large amount of header files?

As I have worked on my program, I have gotten to a point where I have about 30+ header files.

When I started, I had an all-in-one header that linked to all the header files I made up to date, as well as the libraries I needed for the project.

With this format, I had to take 5+ minutes in order to recompile the ENTIRE program as a whole just by changing a variable name or something equally as small.

My previous plan was to make a "Tier List" of all my headers, where the most basic ones are at the bottom, the next level headers required these, so on and so forth until I got to the main.cpp file. (Tier 2 needed the highest of Tier 1 and so on). After this idea, I eventually deviated into making folder groups, and having a Folder.hpp file for if I needed everything in that directory.

Now when compiling, I am immediately overwhelmed with syntax errors saying that nothing is properly included, when the include command is sitting right in front of my face.

Here are some of the headers, as per one of the syntax errors:

===Character.hpp===
#include "../../Pre.hpp"
#include "../Assets/Folder.hpp"
#include "../Control/Folder.hpp"
#include "../WorldObj.hpp"

===Control/Folder.hpp===
#include "CommonStat.hpp"
#include "DeathHandler.hpp"
#include "EnergySys.hpp"
#include "FactionHandler.hpp"
#include "Interaction.hpp"
#include "Warper.hpp"
#include "WeaponPos.hpp"

===WeaponPos.hpp===

struct WeaponPos{};
===Build Message===
Build Message: error: 'WeaponPos' does not name a type

===NOTE===
The #include lines are the only (seemingly) important portions of the code. the 'struct WeaponPos' is only to show where it is located in terms of the syntax error.

WeaponPos myWpnPos is a variable of Character Forgot to inlcude that note.

What would be a good way to properly and effectively organize the header files so they are all accountable when compiling a source file?

I am using Code::Blocks and MinGW GCC 4.7 on Windows 7 64-bit.

Upvotes: 2

Views: 383

Answers (1)

lisu
lisu

Reputation: 2263

This by no means solves your entire problem, but it is worth knowing that it exists and it may actually help in your future design decisions.

One of the ways of handling long time to recompile caused by a small change is PIMPL idion - general idea is to have a separate struct with all private members of the class. The struct is forward declared in header file and defined in cpp file of the class. Therefore, when you add/rename a field in your private struct, header doesn't change at all, thus other headers linking to it don't have to be recompiled.

Much better description with examples

Another important point is not to create levels of included headers - one should rather include headers only if they are really needed. Good example of this is forward declaration. If you have Class1 as a pointer-member in Class2, you don't have to actually include Class1.h in Class2.h - it is usually enough to forward declare Class1 and then include the header in the Class2.cpp file - which reduces recompilation greatly.

To sum up:

  • use PIMPL
  • forward declare and include in cpp when you can
  • do not include when you don't have to

Upvotes: 1

Related Questions