Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abolish include-files in C++

Tags:

Suppose i have the following code (literally) in a C++ source file:

// #include <iostream> // superfluous, commented-out using std::cout; using std::endl;  int main() {     cout << "Hello World" << endl;     return 0; } 

I can compile this code even though #include <iostream> is commented-out:

g++ -include my_cpp_std_lib_hack source.cpp 

Where my_cpp_std_lib_hack is a file in some central location that includes all the files of the C++ Standard Library:

#include <ciso646> #include <climits> #include <clocale> ... #include <valarray> #include <vector> 

Of course, i can use proper compilation options for all compilers i care about (that being MS Visual Studio and maybe a few others), and i also use precompiled headers.

Using such a hack gives me the following advantages:

  • Fast compilation (because all of the Standard Library is precompiled)
  • No need to add #includes when all i want is adding some debugging output
  • No need to remember or look up all the time where the heck std::max is declared
  • A feeling that the STL is magically built-in to the language

So i wonder: am i doing something very wrong here?

Will this hack break down when writing large projects?

Maybe everyone else already uses this, and no one told me?

like image 950
anatolyg Avatar asked Nov 28 '10 20:11

anatolyg


People also ask

Why don't we include .cpp files?

The cpp files can be compiled ahead of time. This doesn't work in you #include them, as it needs to actual "include" the code into your program each time it compiles it. If you just include the header, it can just use the header file to determine how to use the precompiled cpp file.

Why do we include header files in C++?

Why Do You Use Header Files? Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .

Why do we need header files in C?

The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program. In C, the usual convention is to give header files names that end with .

What happens if we remove header file from C program?

You can't use any of the macros defined in the headers. This can be significant. The compiler can't check that you are calling functions properly since the headers define their parameters for it.


2 Answers

So i wonder: am i doing something very wrong here?

Yes. Sure, your headers are precompiled, but the compiler still has to do things like name lookups on the entire included mass of stuff which slows down compilation.

Will this hack break down when writing large projects?

Yes, that's pretty much the problem. Plus, if anyone else looks at that code, they're going to be wondering where std::cout (well, assume that's a user defined type) came from. Without the #includes they're going to have no idea whatsoever.

Not to mention, now you have to link against a ton of standard library features that you may have (probably could have) avoided linking against in the first place.

If you want to use precompilation that's fine, but someone should be able to build each and every implementation file even when precompilation is disabled.

like image 183
Billy ONeal Avatar answered Dec 16 '22 09:12

Billy ONeal


The only thing "wrong" is that you are relying upon a compiler-specific command-line flag to make the files compilable. You'd need to do something different if not using GCC. Most compilers probably do provide an equivalent feature, but it is best to write portable source code rather than to unnecessarily rely on features of your specific build environment.

Other programmers shouldn't have to puzzle over your Makefiles (or Ant files, or Eclipse workspaces, or whatever) to figure out how things are working.

This may also cause problems for users of IDE's. If the IDE doesn't know what files are being included, it may not be able to provide automatic completion, source browsing, refactoring, and other such features.

(FWIW, I do think it is a good idea to have one header file that includes all of the Standard Library headers that you are using in your project. It makes precompilation easier, makes it easier to port to a non-standard environment, and also helps deal with those issues that sometimes arise when headers are included in different orders in different source files. But that header file should be explicitly included by each source file; there should be no magic.)

like image 45
Kristopher Johnson Avatar answered Dec 16 '22 09:12

Kristopher Johnson