organization of files in a project

Started by
5 comments, last by JohnBolton 18 years, 5 months ago
I'm planning to make a fairly large game(in comparison to my previous projects) next. Currently, I have one .cpp file (i'm using c++) for main() and one more for each of the classes. I then have one .h file that has a bunch of #includes and the declerations for all the classes. All the .cpp files #include this .h file. This is an extremly inefficient way, since if I change the .h file, I have to recompile *everything*. Thats no fun in a large program. I was wondering, how should I set up my next project? Thanks in advance, Ezbez
Advertisement
What I do is for each class and its related classes structs ect... I have a .h and a .cpp for them and I have a main.cpp and a main.h and just use #include in main.h to include the .h files for the classes ect... I need and include main.h in main.cpp.

Maybe someone may have a better way to do it but if this works for you then I am glad I could help.
Gor435 - My Journal - MySpace - Facebook
If you're working with Visual C++, you can try using "Precompiled Headers" which cause a great speed improvement while compiling large projects.

If you're not, you may still use a common header but only for "stable" headers/classes.
By "stable", I mean code you're sure you will not often modify because it's mature.

--
hope my reply is understandable since English is not my native language...
1. For each class, use one .h file and one .cpp file.
2. Learn how to use preprocessor checks and defines to make sure you only include each header file once in each compilation unit.
i.e. do this:

#ifndef SOMEFILE_H
#define SOMEFILE_H

class someclass {
...
};

#endif

3. Don't include headers you don't need..



Thanks! I don't use Visual C++, unfotuntaley.

@ap Your English was great.
Well to start with I have to state that I think this is one of the hardest parts of a large development, or project.

But I guess it all depends on the size of youre project, say are you planning to write a game engine there will probably be a great idea to get the project devided in subprojects, wheras you might just need a main project if the project is not as big. In game engines you often see sub projects for all different parts, (and some times even more sub projects) like a scene graph project, a network project, a renderer project.. and so on

to create a structure are hard, or at least I find it hard. but I think the best way is to start off by sitting down with pen and paper, and define what features you NEED to complete the project and what features you would like to get in there, then you try to find out where you could use polymorfism, where you could use inheritance, how the parts interact in short.

after this you should have a rough model of your system, and from there you would start figuring out what classes you need to realize the features you like to get in there. and get some kind of UML skiss or the like, this will be different from what you will actually create (or at least thats my experiance), but you will have a good chance of finnishing your project if you do a bit of planning beforehand :)

ok, so this is easier said then done, but I try to use this approach and it's ok, still, if there is someone that have a solid method of aranging your project I would love to know of it as well, as I said this is something I find hard.
"Who ever put in this magic number should die, in the face" - found documented in the code of TGE
Read this: Organizing Code Files in C and C++
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement