Question

I have a header file which include itself several external headers like

 #include <boost\shared_ptr.hpp>

I want to keep the header self-sufficient, and also add the external include directives in the precompiled header (I am using the classic stdafx.h setup) to get compilation speed up. Are the redundant include directives removed or at least processed faster in this case??

Was it helpful?

Solution

Using the precompiled header does not speed up the processing of redundant includes. If the redundant include files contain header guards (or #pragma once) they will be processed much faster.

OTHER TIPS

If you want to avoid adding a #include in every cpp file, then you can use from the project properties: Configuration Properties -> C/C++ -> Advanced -> Forced include file option. This will automatically add the specified include in every cpp file in the project.

However, if you still have one or more cpp files that include large headers, or even one header that itself has a long list of includes of other large headers, then the cpp files that include this, will compile slow whether you use precompiled headers or not. One solution, is to add this/these long headers to your stdafx.h

Also, every header should have guards to prevent redundant processing by using the classic portable

#ifndef _MyHeader_H_
#define _MyHeader_H_
... header code here
#endif

or the Microsoft-specific

#pragma once

Having guards when headers include other headers which in turn may include already included headers, will prevent multiple inclusion errors, and speed up preprocessing time.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top