0

I've learned the most basic of C++ but I feel like I don't know how to organise the code and start up a C++ project. I've searched for guides about project/code organization without very much luck.

So I want to find a litle well-coded real program to see how is it organised, but I find nothing. Do anyone know a real well-organised program in C++? Preferably open source, terminal based and unix-only.

Thanks.

NeDark
  • 1,212
  • 7
  • 23
  • 36

5 Answers5

2

This is mostly dependent on a couple of things:

  1. Supported platform(s): this will push or pull you toward different code structure if you abstract the different platforms differently.
  2. Build system: stuff like CMake, qmake, autocr*p, Ant, Bjam, jam, etc...
  3. IDE: this won't be a show-stopper, but still could decide number 2.
  4. Is it a GUI or console only or library project: this will IMO force your to structure your program differently, especially if you obey the unspoken rule of splitting GUI and "processing" functions. A library will need a nice API header set, where another (non-library API bound) project will let you be free in that regard.
  5. What do you think is best? If you don't feel right in a certain set-up, don't use it and refactor (if it doesn't take all your time of course...

I have just started a humble C++ project, with a small platform abstraction layer, maybe that can give you some hints/suggestions. Source code is viewable here (it is quite nonfunctional now and uses qmake to build): http://sourceforge.net/p/ambrosia/git -> browse

What I did: - One platform abstraction header which provides platform-independent function definitions which are implemented in (currently) one source file per platform. - One global header including several headers which contain stuff needed virtually everywhere. - Some subfolders logically organized per goal of the code.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • I could learn quite a lot with your application ambrosia. Thanks! – NeDark Jan 03 '11 at 16:33
  • @NeDark: all right! The thing is useful to someone `:)`... I kept to standard C++ as much as possible, with only a little C style code for simplicity/readability in tiny functions and the OS dependent stuff in `Platform.h/cpp`. Hope you enjoy reading through it! Feel free to use the forum if you have any questions or suggestions. – rubenvb Jan 03 '11 at 20:28
1

Generally, the first thing to do before you start coding is decide on naming conventions.
Variable names, class names, namespaces, method names etc.
Then you have to decide the separation of the code in header files and cpp files and the directory they will reside (same dir or different).
The directory names should be meaningfull (more conventions here) i.e. a class that offers utility methods used by many components should be placed in the subdirectory e.c. /common or /util.
You should decide on versioning system e.g. clearcase.
Also a very important point (IMHO) is how the logging is done. This must be implemented and consistent to all modules.
These are strong points to focus, as in studying a ready project, may be time consuming, since you have to study it quite a bit, to notice all the convention and underlying relation of code. Additionally you would not know why one convention was preferred over another.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
0

You can have the in different folders and use another file to fetch/include both files on file, which you will use as your include. Visit the link below it I show you how to implement your own file organization structure. http://codednotes.blogspot.com/2014/01/organising-headers-and-source-files.html

kanthonye
  • 149
  • 1
  • 4
0

There are no widely-accepted rules about organising the code in a C++ app. I, for example, prefer using namespaces, creating a separate folder for each namespace and putting all the headers and sources related to that namespace into the corresponding folder, thus the project root contains only the file with main(), makefile, and possibly a couple of other files. However, others might have different preferences.

-1

Google's C++ style guide.

I'd suggest taking, for example, a look at Google Chrome's source code. It is large, but since Google made it, and I believe they do take coding standards seriously, it can't be bad to explore a bit.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • 2
    I'm not the downvoter, but I'd humbly point out that the Google style guide doesn't have much to say about file naming conventions, directory structure, build setup, etc. which is what I think the question was getting at. – Derrick Turk Nov 17 '10 at 19:46
  • @Derrick: point taken and noted. I've read that guide some time ago and believed there are such things buried in there. Guess I was wrong. – darioo Nov 17 '10 at 19:48
  • 2
    And many people don't feel at ease with Google's C++ style. – Alexandre C. Nov 17 '10 at 19:48