K
K
kirsan_vlz2011-10-06 19:04:12
C++ / C#
kirsan_vlz, 2011-10-06 19:04:12

C++ source organization?

In the course of learning the language, the programs become more and more and the question of organizing the source codes arose.
Googled for a few hours, but found nothing useful. Highlighted the following options:
1) Everything is thrown into one directory, then the headers are included very simply.
2) Compilation units in one directory, headers in another. Then the inclusions become more complicated due to the transition through the parent directory.
3) The modules are broken, in which the headers and compilation units are on the heap. It's not entirely clear how to link modules.
Still not quite understood with setting the include path in the makefile. If you specify this parameter, then, for example, for #include someHeader.h", the compiler will look for the someHeader.h file not only in the directory of the compiled cpp file, but also in the path specified in the parameter?
And I saw something else about setting prefixes in #include.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
darkslesh, 2011-10-06
@darkslesh

I made such a structure for myself (I often use it in projects if there are more than 3 thousand lines)
1) Everything is in one place
2) C / CPP files contain code, and include “header.h” in the header
3) all H files contain function prototypes , constants and structures that are relevant in the C/CPP file.
4) all header files are written in the header.h file (first system ones, then your own ones)
Thus, it is very easy to edit everything related to one code file (H and CPP files have the same name, the current extension is different). When adding a new module, there is no need to write its header file in each source where it is used, it is enough to write it only in header.h
And besides, this approach makes it easy to bypass situations with mutual include (the first on the second, and the second on the first)

M
MikhailEdoshin, 2011-10-07
@MikhailEdoshin

I read advice at one time, but I didn’t try it myself (my projects are not very complex) - one .hfile is written for the entire project, but each logical section in it is wrapped in

#ifdef USE_A
...
#endif
.
In the source codes, respectively, first it is determined what to use, and then the .h-file is included:
#define USE_A
#define USE_B

#include "project.h"

The author wrote that such a structure is convenient for large projects. True, this book is quite old and also described C.

R
Riateche, 2011-10-07
@Riateche

Use the third option - cpp next to h, modules in subdirectories. Add the project root directory to the include path. Inside the module, the includes are made directly, the includes with other modules are written relative to the root folder.

T
Trrrrr, 2014-02-16
@Trrrrr

here I implemented everything more cunningly:
there is a bin folder - everything compiled gets there.
there is a build folder - there are project files in it (separated by folder under different ideas)
there is a sources folder - there is a folder in it that matches the project names in the syulesh. those. for example, in solution we have 3 projects Core, Render, Game. So in the sources folder we will have 3 of the same folders with the same names. In each of them, both headers and resources are in a heap.
Each project is in its own namespace that matches the name of the lib.
And there is a header with all the inclusions that are taken out from either to the outside, i.e. named Core.h for example. projects have access to each other's data only through one single header.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question