How to logically organize source files in C ++

My source file area is growing rapidly (in terms of the number of files in my project), and it becomes a little cumbersome to quickly find the specific source file that I need to access at any given time. I use Embarcadero C ++ Builder, but I have encountered this problem in other C ++ IDEs as well.

In Java, I often use packages to create logical partitions of my source code, especially when working with a large number of source files in one project. Although this, of course, is not the only purpose of Java packages, they are very convenient in this regard.

Does anyone have any ideas on how I can achieve similar functionality in C ++? Should I separate my source from physical folders? Does C ++ Builder create some kind of virtual folder / grouping functionality that I just don't see? Any ideas are welcome and thank you.

+8
source share
3 answers

I usually recommend not using (only) an IDE or language syntax to organize your source code. First, you become attached to the environment: well organized in the IDE, unorganized in the file, and then the day comes when you may want to use another environment ...

Because of this, I usually use all three methods of organizing my source at the same time: I divide my source into functional modules, that is, related classes. Each module gets its own namespace, physical folder, and IDE folder. (In my case, if necessary, use CMake and source_group() to generate the IDE project files - personally preferring the command line, Vim, and "make".)

Therefore, whether I am looking at a project from the IDE, from the command line, or from the compiler log, foo / some_class.hpp is foo / some_class.cpp is foo :: some_class, minimizing confusion.

In fact, my currently preferred installation further subdivides each module directory into <project>/<module>/<class>.hpp or <project>/<module>/src/<class>.hpp depending on which whether the class is outside its own module or not, <project>/<module>/src/<class>.cpp and <project>/<module>/test/<class>_tu.cpp . The namespace, of course, is <project>::<module>::<class> .

 project |-- foo | |-- some_class.hpp | |-- src | | |-- internal_class.hpp | | |-- internal_class.cpp | | '-- some_class.cpp | '-- test | |-- internal_class_tu.cpp | '-- some_class_tu.cpp |-- bar | |-- ... 

The idea is that the "external" interface of each module ( foo ) is documented by the headers in this subfolder, and implementation details and tests are "hidden" in the corresponding subfolders.

But in the end, it very much depends on your taste, the taste of your co-developers and the scale of your project.

+9
source

This is how I roll:

 PROJECT_NAME |-- build // This is DVCS ignored but has all the built intermediates and final binaries | |-- release // These are the different build profiles | |-- debug | |-- profile | `-- coverage |-- bin // For binary source code | `-- hello_world | |-- doc | |-- inc | |-- src | |-- tests | `-- build_script // Builds binary into the build folder |-- include // Public headers for the library | `-- these | `-- folders | `-- represent | `-- namespaces | `-- my_awesome_class.hpp |-- lib // library source code | |-- these | | `-- folders | | `-- represent | | `-- namespaces | | |-- inc // Private headers | | | `-- my_private_class.hpp // internal class | | |-- src // Source code for this namespace | | | |-- posix | | | | `-- my_awesome_class.cpp // posix specific source code | | | |-- nt | | | | `-- my_awesome_class.cpp // nt specific source code | | | |-- my_private_class.cpp // non-visibile class | | | `-- my_awesome_class.cpp // cross platform source code | | |-- tests // Unit tests | | | `-- my_awesome_class.cpp // builds a test executable for the library | | `-- doc // Documentation for this namespace | | `-- namespace.dox | `-- build_script // Builds binary into the build folder |-- doc // Documentation files | |-- main_page.dox | `-- namespace.dox `-- build_script // Builds the source code into the build folder 

This is the class these::folders::represent::namespaces::MyAwesomeClass , which has posix and NT specific source code (as well as common source code), plus there is a closed these::folders::represent::namespaces::MyPrivateClass , which used inside the library, headers are not public and visibility class characters.

It scales very well and provides easy file detection.

+10
source

I make projects so that all my files are easily accessible. This is the easiest way to organize as well as clear class / file names.

0
source

All Articles