One header for multiple cpp files

Suppose I have 3 cpp files:

Main.cpp First.cpp Second.cpp 

Now I want to use the functions from First and Second in Main. What to complain if I used only one header file for both cpp files and their functions?

Could there be any problems when using the header file in First, because I need a function from Second in First?

+6
source share
5 answers

As long as you run ODR, one rule of definition , and each declaration will be consistent, you'll be fine.

Headings are usually intended to provide declarations of functions and other materials, the binding phase (unless there is something more complex, such as templates) will take care of eliminating these dependencies.

I don't say anything about design - not enough information.

+5
source

No, if you provide only one definition for each declaration to the compiler, this will be fine.

However, it is best to provide one source file in the header file: if "First" is needed by both, you simply include First.h and Second.h

+1
source

As for C ++, there will be no problem using a single header file and splitting the actual function implementations between the two files. However, if you need functions from "Second.cpp" in "First.cpp", you might want to see what happens in each of them, and maybe the functionality should be split into another file.

As a personal preference, I say that you should stick with one cpp file for each header file. Splitting the implementation will simply cause headaches later if you need to re-execute the implementation again.

0
source

Just try to say if you are using an architecture in which you mix server and client codes in one definition, separate them with different cpp files.

0
source

One header, several cpps. All functions in Cpps can be declared in one header file. (Example: "general.h") And all Cpps should include the file "general.h". Then define the bodies of the declared functions.

0
source

Source: https://habr.com/ru/post/969961/


All Articles