Organizing header files

Started by
13 comments, last by Muhammad Haggag 19 years, 7 months ago
How do you organize header files? If you have a complex file hierarchy like this:

/project
  /graphics
    /particlesystem
    /terrain
  /physics
    /collision
    /water
    /ragdoll
managing paths in #includes can get quite annoying. I'd rather write

#include "terrain.h"
than

#include "graphics/terrain/terrain.h"
One obvious solution is to run a script at the beginning of every compilation that grabs all header files and puts it under /project/includes. The problem with this is that when there are errors in the header files, the IDEs bring up a file from /project/includes when you try to look at the error. You end up modifying a temporary file that gets overwritten the next time you compile which is obviously a large pain in the ass. How do you handle this situation?
Advertisement
You can set additional include directories in your project so directory structure won't matter to the compiler.
Quote:Original post by Maega
You can set additional include directories in your project so directory structure won't matter to the compiler.


To make this easier to manage, it's also a good idea to put the most common headers in a single include directory or in a single static library called CoreLib or something like that.

Well, adding many compiler directories also isn't a very good solution...

Does GCC support precompiled headers?
Quote:Original post by CoffeeMug
Well, adding many compiler directories also isn't a very good solution...

Does GCC support precompiled headers?


And writing a batch file is? :/


GCC should support precompiled headers.
Quote:Original post by Maega
And writing a batch file is? :/

Took me 15 minutes after Olusei (damn, his name is hard to spell) recommended Python.
Quote:Original post by CoffeeMug
Well, adding many compiler directories also isn't a very good solution...

Some IDEs+compilers like CodeWarrior automatically do this. Which is inappropriate, IMO.

I ran into problems where I had files with the same name in different project subdirectories: 2 "common.h" files. CodeWarrior was always including the wrong one because it searches the project paths in a certain order, and includes the one that comes first.

So generally, I agree with this point

Quote:Does GCC support precompiled headers?

As far as I know, yes. You'll definitely find more details on the gcc website.

Quote:How do you handle this situation?

I usually use relative pathing, e.g. "graphics/terrain/terrain.h"
It's not that bad - it forces you to think your directory structure before doing anything, which is good for things like CVS anyway.

Quote:Original post by CoffeeMug
Took me 15 minutes after Olusei (damn, his name is hard to spell) recommended Python.

Not really that hard. Just stop for a minute, learn the pronounciation of his name by heart, and then you'll be able to write it in a sec: Oluseyi.

I tried to learn the spelling first, but that led me to nowhere [smile]

I don't see what is so annoying about putting the paths in the #include. What is there to manage?

It helps if the organization of folders in a project mirrors the organization of the software. The source file and its header file should be kept together. Splitting them into separate folders seems like a very bad idea.

Keep the number of folders in the include path to a minimum. On a large project, you easily have more than 100 folders. Putting all those folders into the include path is going to cause problems.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by Coder
Just stop for a minute, learn the pronounciation of his name by heart, and then you'll be able to write it in a sec: Oluseyi.

I can't even begin to imagine how his name is pronounced [smile]

This topic is closed to new replies.

Advertisement