C++ files organisation

Started by
11 comments, last by BitMaster 9 years, 9 months ago

Avoiding lots of search pathes on the command line has the benefit that it is clear what version of a header file is used and you need not read hundreds of -I parameters with long pathes to find at which point something goes wrong.

But as I already said. It is always alot of work if you move a module around.

Advertisement

So instead of relative includes, what would be a good way if I don't want a centralized include folder ?
I have this problem recently when I was reorganizing my files and I need to update quite a few of the includes.


Unless you are making reusable libraries as part of your project, I would recommend a single folder for both your src and header files. i.e a single .exe means I would create a single src directory. If you find this folder simply has too much source code files in, then this might even suggest that you need to break your project up into multiple separate libraries (in which case they would get their own src directories (containing .cpp and .h).

So since these libraries and binaries are separate projects, you could say that I don't do any nesting in my projects. Some places where you may be tempted however is if some code is completely standalone from the rest of the project (i.e so no #include "../" needed since it has no dependence on other headers). If a part of a project is also in a separate namespace then you may also want to nest, however often parts in a nested namespace still require ../headers and I do typically try to avoid this pattern.

One system that I have found to be very effective is the following (I use cmake but this should work with many build systems). Imagine a folder structure as follows:

proj/
  src/
    game/
    foolib/
    barlib/
If I specify on the command like -Isrc, this means that anywhere in the game source code, I can do

  #include <foolib/foolib.h>
If foolib has a dependency on barlib, I can do in the foolib code:

  #include <barlib/barlib.h>
This means that you can separate your project into logical libraries (and separate .cpp / .h directories) and yet still be able to reference the correct headers you need.

Whats quite useful about this system is that if barlib was really made to be a standalone library, I could have an installer script like:

# mkdir /usr/local/include/barlib
# cp -r src/barlib/*.h /usr/local/include/barlib
# cp lib/barlib.a /usr/local/lib/
And now any project on my computer can access the barlib.h in exactly the same way as when it was part of my project.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

Avoiding lots of search pathes on the command line has the benefit that it is clear what version of a header file is used and you need not read hundreds of -I parameters with long pathes to find at which point something goes wrong.


Well, in general I set exactly one include directory for my project (excluding 3rd party libraries). Every file can then simply include what it needs using <mytool/file.h> or <mylibrary/file.h>. CMake just simplifies doing that because every sub-makefile is aware of that include directory without any work on my part. An added benefit is that you immediately see which library/subproject an include is from.

This topic is closed to new replies.

Advertisement