2

In my project several STL headers are used in different files. I read that, putting all these headers into a single header and using that header in my files will allow compilers to precompile the header which may lead into faster compile time.

If I understood it correctly, I need to write like the following.

// stl.hpp
#include <string>
#include <algorithm>
#include <vector>

Now include stl.hpp in all the files that needs access to STL. Is this correct?

Few of my files will be using only functionality from vector header file. But if I follow the above method, it will include unnecessary headers. Will this make any problem? Is there any code generated if I include a header file and not used anything from that?

Any help would be great!

6
  • I'm highly skeptical about the claim that including all the header files in a single header file will lead to faster compile time. Commented Nov 14, 2009 at 18:43
  • C++ Templates - The complete guide says, it does. I am not sure though. Commented Nov 14, 2009 at 18:46
  • 2
    It is pretty easy to time the compilation, just give it a try to see if there is significant improvement. Commented Nov 14, 2009 at 18:51
  • stl.hpp is probably a misnomer. Whether <string> is part of the STL is at least debatable. (Note: The STL was a container/iterator/algorithm library, developed by Alexander Stepanov. It was incorporated into what was, until then, the draft C++ standard library, delaying the standardization of C++98 for about one year. While doing so, the language and the rest of the library was extended and the STL was changed, so that it all would fit together better. Among others, std::basic_string got a container interface, templates where extended, and the STL adopted to use the extended templates.) Commented Nov 15, 2009 at 17:04
  • 1
    @Charles Including the headers in a single header file isn't what speeds up the compilation; what speeds it up is that all of the pre-compiled headers are scanned and compiled once and never looked at again during subsequent builds (assuming they haven't changed in the interim). This can provide significant speed-ups depending on the number and size of header files in your project. Commented Nov 18, 2009 at 18:22

2 Answers 2

2

Basically every decent compiler uses precompiled headers. Already compiled headers will be cached and only recompiled if they were changed.

Using already compiled headers instead of recompiling them every time speeds up compilation time.

But whether you combine commonly used headers in a singe file or include them in each source file separately won't matter in terms of compilation speed.

Sign up to request clarification or add additional context in comments.

Comments

0

Before attempting to speed-up you build by using pre-compiled headers, it's worth benchmarking/timing your existing builds to see if the speed-up will be worth the effort.

If you only have a few dozen files with #include <string> you may see no improvement. If you have 1000s of file, then it may be worth it.

See this article for more excellent info: www.cygnus-software.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.