27

I would like to know what are C++ best practices when it comes to organizing my project. I've read that I should put all the source files (.cpp) in the src folder and header files (.h) should be placed in the include folder. Is it the way it is supposed to be, or should I put my header files in the source files folder?

This is my folder tree structure

- Project
|
+--- src (.cpp)
|
+--- include (.h) ????
|
+--- test (cpp unit test)
|
+--- doc (docs)

2 Answers 2

16

It is a matter of preference really but organization of a code base helps for maintainability as well as for easily understanding the code. One should have an Modular approach as much as possible. Your code organization just looks about almost right, Ideally I would have:

- Project
|
+--- src (.cpp)
|
+--- include (.h) ????
|
+--- test (cpp unit test)
|
+--- doc (docs)
|
+--- bin (generated binaries)
|
+--- lib (external dependencies)
12

header files (.h) should be placed in the include folder.

Not all of them. Only those in the public domain. If a class or a function is specific to a module, it should be inside that module. The include folder should contain headers that can be included by any other module.

You'll also probably want to add:

  • bin - for keeping your libraries
  • lib - same
  • exe - generate executables here (optional, can be also in bin)
  • etc - configuration files

Not the answer you're looking for? Browse other questions tagged or ask your own question.