Sign in to follow this  
Extrakun

How to you organise your cpp and header files?

Recommended Posts

So I am working on a new project and trying to come up with ways to organise my cpp and header files. I am using VS 2005, breaking up different functionality of the project into different smaller projects inside the solution. I was originally thinking of 1) have the .h file together with the .cpp OR 2) have a folder called src, and another called include, and have a parallel structure that looks like this: \src\Data \src\Modules . . . \include\Data \include\Modules I am not writing a library (but some of the functionalities may be reused by other projects later)...which approach is better for the long run? Thanks in advance!

Share this post


Link to post
Share on other sites
After a couple of years I decided (at least for now) that the best way to organize my source files is .h and .cpp in the same folder, and the directory structure organized exactly as the namespaces defined in the program/library (A contains B contains C).

Share this post


Link to post
Share on other sites
I tend to just dump all source files in a single directory, and let IntelliSense deal with it. I used to do the whole nested-directories thing, but I don't find it worth the effort to maintain these days - at least for C++: languages with a proper package mechanism (such as Python) are another story.

Share this post


Link to post
Share on other sites
I tend to do the same thing as owl: group .cpp and .h files together and mirroring the namespace structure. Intellisense often breaks on templated code, so dumping everything into one directory doesn't work very well for me.

Share this post


Link to post
Share on other sites
include/src with parallel directory structures for now. This is mostly to cater to my own sense of structure: when #include-ing files, I'd rather see something like #include "resources/cache/resource_cache.h" than simply #include "resource_cache.h". This way I can get a sense of which systems depend on which other systems just by looking at the directories from which they pull their #includes.

Share this post


Link to post
Share on other sites
I keep a...
* include folder for shared headers(without extension) & template implementations
* a source folder for non-shared headers(with extension) and source folders
Both have sub-directories roughly based around project/namespaces (the enum hack comes to mind as an exception)

Share this post


Link to post
Share on other sites
Quote:
Original post by owl
After a couple of years I decided (at least for now) that the best way to organize my source files is .h and .cpp in the same folder, and the directory structure organized exactly as the namespaces defined in the program/library (A contains B contains C).


Exactly, its what I use as well...almost by default I done it that way for the first time. If you give a class a category (namespace) it makes very much sense to put it in the same category directory wise.

Share this post


Link to post
Share on other sites
I have found over the years that there are very strong advantages to mimicing the C++ namespace heirarchy with a source file namespace heirarchy. This eliminates filename conflicts the same way it does variable and type names.

I have also found the urge to place headers and sources in separate directory heirarchies somewhat weird and unjustifiable. Sources and headers go together. When generating a dev package for a library the headers get pulled and packaged separately anyways.

Generally, assets are kept in a separate directory heirarchy because they are run-time data not build-time data.

Packaging metadata is also stored separately from sources. This simplifies making targeted source distributions.

Finally, a separate heirarchy for test suites are separate from sources. Testing should be orthogonal to building and packaging.

Share this post


Link to post
Share on other sites
Quote:
Original post by caldiar
I toss everything in one folder and use filters in Visual Studio to organize them within the IDE.


Mmm. That's exactly what I do. I tried the namespaces / folders approach, but found that irritating for some reason (full relative path shown in the visual studio solution explorer or something, IIRC).

Does putting them in different folders have any affect on how intellisense deals with the files? (I find it's mainly too broken to be useful).

Share this post


Link to post
Share on other sites
My .h and .cpp files exist in the same folder, however I have a folder for each namespace they exist in.

/src/core/console.h
/src/core/console.cpp
/src/core/engine.h
/src/core/engine.cpp
/src/visual/render_subsystem.h
/src/visual/render_subsystem.cpp

So is appears like this in the code:

#include "core/console.h"
#include "core/engine.h"
#include "visual/render_subsystem.h"

core::console console;
core::engine engine(&console);
visual::render_subsystem* render_subsystem = new opengl_render_subsystem();
// etc...

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
I have found over the years that there are very strong advantages to mimicing the C++ namespace heirarchy with a source file namespace heirarchy. This eliminates filename conflicts the same way it does variable and type names.

I have also found the urge to place headers and sources in separate directory heirarchies somewhat weird and unjustifiable. Sources and headers go together. When generating a dev package for a library the headers get pulled and packaged separately anyways.

Generally, assets are kept in a separate directory heirarchy because they are run-time data not build-time data.

Packaging metadata is also stored separately from sources. This simplifies making targeted source distributions.

Finally, a separate heirarchy for test suites are separate from sources. Testing should be orthogonal to building and packaging.


I agree with all of that except "Sources and headers go together".

IMHO, public (i.e. interface) headers should be in a seperate directory. For me, this makes it easier to package up a lib.

That said, I've mostly abandoned C++ these days and the header/source split is something I'm thankful I don't have to deal with any more.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this