User forums > General (but related to Code::Blocks)

Project Structure -- where should I place header and source files?

(1/3) > >>

namnet:
Hello all!

I'm fairly new to both C++ and Code Blocks, so forgive me if my question is stupid -- I simply wonder how to best structure a projects source and header files.

Let's say I have two namespaces and three classes:

app
- GameState

scene
- World
- Entity

... and a file structure that looks like this:

project_home/app/GameState.h
project_home/app/GameState.cpp

project_home/scene/World.h
project_home/scene/World.cpp
project_home/scene/Entity.h
project_home/scene/Entity.cpp

project_home/main.cpp

Now, this gets ugly if GameState has to include World.h (#include "../scene/World.h")

Tracing back the path like this simply feels wrong. How should I avoid it? Keep all my files in the same directory? Put all the headers in an include directory (project_home/include/app/GameState.h) and tell the compiler about it?

How do you do it?

I'm actually used to letting the IDE manage this for me by using some sort of "add class" function, but for all my life I can't find anything like that in Code Blocks.

Thanks in advance!

killerbot:

--- Quote ---Now, this gets ugly if GameState has to include World.h (#include "../scene/World.h")

Tracing back the path like this simply feels wrong. How should I avoid it? Keep all my files in the same directory? Put all the headers in an include directory (project_home/include/app/GameState.h) and tell the compiler about it?
--- End quote ---

Keep your seperation as you have it. In your project file you specify the include paths of the headers. These are passed on to the compiler as command line options (typically with /I<myDir>), and i nthese paths the compiler will hunt for the header file specified as :
#include "world.h" /// <---- so no extra dirs in here

Tip : specify your paths relatively to the project.

Cheers,
Lieven

namnet:
Wow, that was the swiftest answer I've ever gotten! Thank you :)

I actually want have it like #include "scene/World.h" instead of "../scene/World.h", so I can't do as you described above.

killerbot:
If you want to do it like that, then you have to add the directory containing the scene directory as an include directory to your project.

But if you ever want to restructure your project you will have to change :
 - the include directories of your project (obviously)
 - the code #include "scene/world.h"   -> #include "restructuredscene/world.h"

You can get rid of this latter dependency by adding the scene directory as include dir.

namnet:

--- Quote from: killerbot on March 08, 2006, 08:27:16 pm ---If you want to do it like that, then you have to add the directory containing the scene directory as an include directory to your project.

--- End quote ---
Yes, I tried doing that but unfortunately the directory that contains the scene directory is my project home, which seemed to make the compiler flip out. I guess I could work around this by adding an extra directory layer, such as "myApp/scene/World.h".


--- Quote ---But if you ever want to restructure your project you will have to change :
 - the include directories of your project (obviously)
 - the code #include "scene/world.h"   -> #include "restructuredscene/world.h"

You can get rid of this latter dependency by adding the scene directory as include dir.

--- End quote ---
Right... I'm beginning to see the upside of doing it that way... A downside is that as soon as I want to add a new directory I'll have to go add it as well.

Thank you for your answers!

Navigation

[0] Message Index

[#] Next page

Go to full version