We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this questionI'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.
This is mostly dependent on a couple of things:
- Supported platform(s): this will push or pull you toward different code structure if you abstract the different platforms differently.
- Build system: stuff like CMake, qmake, autocr*p, Ant, Bjam, jam, etc...
- IDE: this won't be a show-stopper, but still could decide number 2.
- 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.
- 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.
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.
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.
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
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.
精彩评论