Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source code layouts in C++ [closed]

Tags:

c++

I'm new to C++, background in Java and Python, source code file layouts in those language are pretty standard. However I've noticed with C++ projects, there seem to be at least 3 different types of layout:

Option 1 All files (.hpp and .cpp) in the same directory

---main folder
   |
   --- file1.hpp
   --- file2.hpp
   --- code1.cpp
   --- code2.cpp

Option 2 Headers (.h .hpp) all in a single include directory and implementations (.cpp) in a src directory

---main folder
   |
   --- include
   |   --- file1.hpp
   |   --- file2.hpp
   |
   --- code
       --- code1.cpp
       --- code2.cpp

Option 3 Headers (.h .hpp) all in a single include directory with specific directories for different groups of headers (details, impl), all implementations (.cpp) in a single dir

---main folder
   |
   --- include
   |   --- file1.hpp
   |   --- file2.hpp
   |   --- directory1
   |   |    --- code1.hpp
   |   --- directory2
   |        --- code2.hpp
   |
   --- code
       --- code1.cpp
       --- code2.cpp

Option 4 Similar to 3 but the implementations also have their down directories.

---main folder
   |
   --- include
   |   --- file1.hpp
   |   --- file2.hpp
   |   --- directory1
   |        --- code1.hpp
   |   --- directory2
   |        --- code2.hpp
   |
   --- code
   |    --- code1.cpp
   |    --- code2.cpp
   |    --- directory1
   |    |    --- code1.cpp
   |    --- directory2
   |         --- code2.cpp

I've had a quick read of the standard and can't seem to find any suggestions or guidance on the matter, the same goes from cppref documentation.

Is there some common layout? or is it a situation of: "what works for you"

like image 750
Jacobi John Avatar asked Nov 17 '16 21:11

Jacobi John


1 Answers

It really just depends on how large the project is, and whether or not you want it used as a library. If it is a library you would definitely split the headers out. For a normal executable project I normally start off just lumping everything in one directory and running g++ to compile the lot, as the project grows and you add a makefile and what not you start adding directories to keep it tidy. In most really large projects I would expect #4. But for really small projects, #1, and that is if they even bother to split the header out of implementation.

like image 130
pteronewone Avatar answered Oct 30 '22 04:10

pteronewone