Reputation: 133
Im new to learning c++ and I was curious as to what the purpose of multiple header files is. Why can't you just have everything in one header file?
Upvotes: 1
Views: 1341
Reputation: 42182
In addition to all the answers already posted, you may want to consider that C++ has a philosophy of you pay for what you use or, conversely, you don't pay for what you don't use.
If my application needs, say, complex numbers, then I go ahead and #include
this functionality. If I don't need it, then there is no reason whatsoever why my code should even be aware that such concept exists.
When I determine that I keep including the same header files over and over again for a given kind of application, I create a master header file which includes the includes, and I just #include
that one.
For example:
// @file project.hpp
// @brief provides all the functionality required by `project`
#include<library1.hpp>
#include<library2.hpp>
#include<library3.hpp>
// any type definitions go below this line
and then, in my project
// @file project.cpp
// @brief Implements `project`
#include<project.hpp>
// go ahead and implement project
Other libraries such as boost
normally offer a "master" include file such as the one described above to simplify using the library.
Upvotes: 1
Reputation: 27934
Headers exist to organize.
For a very simple project, one header file would do just fine. You can even not use headers at all and just write everything in a single file.
But once you grow to a bigger project full of parallel systems, classes that are used in multiple contexts, or simply using 3rd party libraries like OpenSSL or MySQL, you can't simply expect to work putting all this code in a single file with millions of lines of code.
You may also be wondering why not put all and any required header in your project in a single "common.h" and then #include
it in every .cpp
.
The answer is because each .cpp
is compiled individually, so if you limit the headers for each .cpp
to only what is required for it, you will decrease both the resulting .obj
size and the compile time.
Also, you would be forced to recompile the entire project every time you make a change to any header, instead of recompiling only the .cpp
involved.
Upvotes: 2
Reputation: 2675
The keyword here is modularity:
Modularity is the degree to which a system's components may be separated and recombined.
As others have already mentioned, you can write a program in a single file. Moreover, you can write it in a single line. However, you don't do this because it would then be really difficult to separate and recombine parts of this program, let alone debug it if it doesn't compile.
Upvotes: 4
Reputation: 26439
Why can't you just have everything in one header file?
If you cram everything into single header, every single change to that header will cause recompilation of every file that includes that header. In your case that'll be entire project.
It is easier to manage project, when you have many small files and include them only when they're absolutely necessary.
Upvotes: 3
Reputation: 477640
You can have everything in one header file. You can even have your entire program in one single file.
The benefit of separating things out into distinct, small files is that you compile small parts at a time, and you only need to recompile those parts whose components have changed. You can also put common code into separate files and use those files from separate projects, without the need to copy-paste code across projects. So if you find a bug, you can fix it once and all your projects benefit from the fix.
Upvotes: 1
Reputation: 225242
Yes, you can have everything in one header file. However, as your project grows, that header file will soon become an unmaintainable and slow-to-compile mess.
Upvotes: 0