Reputation: 15501
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!
Upvotes: 2
Views: 2404
Reputation:
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.
Upvotes: 2
Reputation: 6150
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
Upvotes: 0