1

Would it be optimizable in a large program if a .cpp file loaded all the needed headers for the application rather than pre-loading it in the main source file?

Like instead of having Main.cpp

#include <header.h>
#include <header1.h>
#include <header2.h>
#include <header3.h>
//main code

Can I just have a .cpp file that does this and just loads .cpp file in the main.cpp? Like this

Loader.cpp

#include <header.h>
#include <header1.h>
#include <header2.h>
#include <header3.h>

Main.cpp

#include "Loader.cpp"
//main code
CraftedGaming
  • 499
  • 7
  • 21
  • 5
    This is a compile-time matter. There's no "header loading" in C++. – LogicStuff Jan 15 '17 at 09:31
  • 3
    Such "super-headers" (as they are sometimes called) are a symptom of programmer laziness. Yes, sometimes compilers implement approaches that make such things faster, but not all compilers do. I remember one developer who insisted he needed substantial hardware to do even incremental builds. An audit found he was using such a super-header. Eliminating the super-header and editing affected compilation units (some days work) to only include headers they needed, reduced complete rebuild times by a factor of about 85. Complete rebuilds could then be done every night if needed. – Peter Jan 15 '17 at 10:11

3 Answers3

6

Preprocessing simply generates the text that gets compiled. Your suggestion leads to the same body of source code, so it will have no effect on optimization.

Including all the headers, all the time (call it a "super-header") may lead to slow compilation.

However, precompiled headers are a popular solution to allow such super-headers to work quickly. You might check your IDE or compiler's documentation to learn its precompiled header facility.

In any case, typically the super-header is still named with .h; since it implements nothing a .cpp name would not be appropriate.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
0

You can, but you may want to reconsider.

You may have trouble with accidentally trying to compile Loader.cpp by itself since you've named it as if it was a source file. Since you're using it as a header - and it is a concatenation of multiple headers - it would make sense to name it according to the convention and use .h file name extension.

Would it be optimizable

It would have zero effect on the compiled program, so in that sense optimization is irrelevant.

This will bring problems with compilation speed however. And those problems are not "optimizable". Copying every header into the source file - needed or not - slow the time (and the slowdown is bound to the hard drive speed) needed to compile the source file. Much bigger problem is that it prevents incremental building because a change in any header file would force the source file to be recompiled, because its content will have changed.

These problems are compounded over multiple source files assuming you intend to use this "technique" with all source files that you have. Having to recompile the entire project when any header is modified is usually non acceptable for any project that is not trivially small.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

It really depends on the compiler. Visual studio has a thing called stdafx.h.

What's the use for "stdafx.h" in Visual Studio?

Community
  • 1
  • 1
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48