-
August 3rd, 2010, 03:49 PM
#1
Why don't set every header file as precompiled ?
Hi there!
I've a quick question about precompiled headers. Why don't set every header file as precompiled in order to reduce compilation time ?
Has a precompiled header any disadvantages
I know that rebuild option ignores this kind of header, but we can always delete the *.pch header file before rebuilding
Moreover, my wxDev-C++ IDE (with the MinGW compiler) is able to even build a precompiled header if somebody modified it...
Last edited by Quentin026; August 3rd, 2010 at 03:51 PM.
-
August 3rd, 2010, 05:31 PM
#2
Re: Why don't set every header file as precompiled ?
inline functions can not be precompiled. template functions can not be precompiled.
-
August 4th, 2010, 07:57 AM
#3
Re: Why don't set every header file as precompiled ?
The main idea of precompiled header is: build it once, use for many .cpp files. For example, project contains 20 .cpp files, each of them starts from #include "stdafx.h". Precompiled header is built in the beginning of the build, and then applied to every .cpp file, reducing significantly compilation time.
However, rebuilding of precompiled header itself takes its own time. Therefore, it is recommended to include only system and third-party headers to stdafx.h. If you include your own h-file to stdafx.h, every change in such h-file results full project rebuild.
-
August 4th, 2010, 08:24 AM
#4
Re: Why don't set every header file as precompiled ?
Originally Posted by Alex F
If you include your own h-file to stdafx.h, every change in such h-file results full project rebuild.
Not exactly...
Like I said before, the MinGW compiler is able to build this kind of header. The full project rebuilding isn't necessary...
-
August 4th, 2010, 09:17 AM
#5
Re: Why don't set every header file as precompiled ?
[QUOTE=Quentin026;1959227]Hi there!
I've a quick question about precompiled headers. Why don't set every header file as precompiled in order to reduce compilation time ?
Has a precompiled header any disadvantages
...
[QUOTE=Quentin026;1959227]Hi there!
A precompiled header normally is used for more cpp files. If you have an own precompiled header for each cpp (what means an additional .cpp that creates the pch), precompiling has no benefit but only overhead.
So, we should look at the standard case where nearly each cpp includes the one header which needs to be precompiled. Then any change of a header that was included by the precompiled header or a change on the precompiled header itself leads to recompiling of the pch and to recompiling of all cpp's which use the pch. Depending on your 'include strategy' which normally should be minimal and complete, i. e. a source only includes those header files which it definitively was using and doesn't rely on other headers to be included before, that could be an overhead cause normally a change on a subheader should only reflect on cpp's which were using that header.
Some more disadvantages:
- if using a versioning system where headers might be changed by other developers, you not necessarily must get a new timestamp for your source when getting new updates. If not, the changes from others might get be ignored as long as you don't make a rebuild.
- all statements added above include of precompiled headers were ignored by the compiler. That implies macros which were set at beginning to get different code compiled. Any change of those macros don't reflect on definitions of the pch. Also changes of project macros don't mark the pch for update with possible strange effects.
All the above (and some bugs involved with pch) made me to not using precompiled headers. It is the first which I do switch off for any new project.
Originally Posted by Quentin026
Moreover, my wxDev-C++ IDE (with the MinGW compiler) is able to even build a precompiled header if somebody modified it...
That is the minimum what each compiler providing pch must serve.
Regards, Alex
-
August 4th, 2010, 09:50 AM
#6
Re: Why don't set every header file as precompiled ?
Just FYI, wxDev-C++ is no longer supported, it got merged with Code::Blocks.
-
August 4th, 2010, 10:27 AM
#7
Re: Why don't set every header file as precompiled ?
Originally Posted by Quentin026
Not exactly...
Like I said before, the MinGW compiler is able to build this kind of header. The full project rebuilding isn't necessary...
That isn't a question of rebuilding the pch. All objects that have that pch included must be rebuilt as well - or they wouldn't be able to reflect on the changes and might have wrong header offsets which are not up-to-date. The latter happens for example if you add a member to a class defined in the header. Then it definitively would crash your program if there would be at least one cpp that was built with the new pch but calls into a cpp which was not and both share instances of the changed class. The only chance for the project builder to avoid that is to recompile all sources that were using the pch.
Regards, Alex
-
August 5th, 2010, 02:35 AM
#8
Re: Why don't set every header file as precompiled ?
Originally Posted by Quentin026
Has a precompiled header any disadvantages
I've never experienced any specific advantage with precompiled headers so I've stopped using them altogether. I include quite a lot of windows and standard C++ and boost headers in every .h file I've made myself and this doesn't seem to impact my compilation times a bit. This is the content of my globals.h which every other .h file includes.
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN // exclude rarely-used stuff from Windows headers
#define NOSERVICE
#define NOMCX
#define NOIME
#define NOSOUND
#define NOCOMM
#define NOKANJI
#define NORPC
#define NOPROXYSTUB
#define NOIMAGE
#define NOTAPE
#include "windows.h"
#include "commdlg.h"
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <assert.h>
#include <array>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <functional>
#include <string>
#include <iostream>
#include <sstream>
#include "boost/smart_ptr.hpp"
#include "boost/enable_shared_from_this.hpp"
#include "boost/foreach.hpp"
#include "boost/utility.hpp"
I always try to minimize the interdependencies in my program and I've never had any problems with long compilation times. I also use multithreaded compilation. On my quad system four .cpp files are compiled at the same time so it speeds up compilation four times.
Aren't precompiled headers something of a hoax really? Don't you have to seriously mismanage your header files for precompiled headers to have any impact at all? It's like first you create a problem but instead of resolving it you apply a band-aid to hide it. Keeping down program interdependences and fully utilizing all available cores seems to be a much sounder approach to fast compilations.
Last edited by nuzzle; August 5th, 2010 at 02:53 AM.
-
August 5th, 2010, 05:18 PM
#9
Re: Why don't set every header file as precompiled ?
Originally Posted by itsmeandnobodyelse
A precompiled header normally is used for more cpp files.
Why is that ?
A precompiled header is - as the name says - precompiled (we have to compile it only once), so no matter in how many .cpp or .h files we'll include it - a compiler won't compile this kind of header again... That is my opinion.
Originally Posted by itsmeandnobodyelse
If you have an own precompiled header for each cpp (what means an additional .cpp that creates the pch), precompiling has no benefit but only overhead.
Why does a .cpp file need to create the .pch ?
In wxDev-C++ if I want to create the precompiled header, I create a normal header and check "Create precompiled header" option...
-
August 6th, 2010, 08:17 AM
#10
Re: Why don't set every header file as precompiled ?
Originally Posted by Quentin026
In wxDev-C++ if I want to create the precompiled header, I create a normal header and check "Create precompiled header" option...
Yes, but then your header may not have inline function, template functions, or macros. All of them are extremely common to find in header files.
Last edited by ninja9578; August 6th, 2010 at 08:20 AM.
-
August 6th, 2010, 11:07 AM
#11
Re: Why don't set every header file as precompiled ?
Originally Posted by Quentin026
Why is that ?
A precompiled header is - as the name says - precompiled (we have to compile it only once), so no matter in how many .cpp or .h files we'll include it - a compiler won't compile this kind of header again... That is my opinion.
Why does a .cpp file need to create the .pch ?
In wxDev-C++ if I want to create the precompiled header, I create a normal header and check "Create precompiled header" option...
Yes, but there is a limit to how much faster a compilation is, when using pre-compiled headers. If you only have a handful of source files, it may not make any difference. On the other hand, if you have a lot of source file, it can make a big difference. One project I worked on, properly enabling pre-compiled headers cut compile time in half (from about an hour to 25 minutes)!
Viggy
-
August 6th, 2010, 11:40 AM
#12
Re: Why don't set every header file as precompiled ?
Originally Posted by ninja9578
Yes, but then your header may not have inline function, template functions, or macros.
Then why can a precompiled .cpp file contain those things ?
-
August 6th, 2010, 12:27 PM
#13
Re: Why don't set every header file as precompiled ?
Originally Posted by Quentin026
Then why can a precompiled .cpp file contain those things ?
The source file is not pre-compiled. The compiler needs to know what source file to use to create the pre-compiled header. All it does is read all the include files in the source file you specify, and creates the pre-compiled header based on that.
Viggy
-
August 6th, 2010, 03:13 PM
#14
Re: Why don't set every header file as precompiled ?
Originally Posted by Quentin026
Then why can a precompiled .cpp file contain those things ?
It was my mental shortcut I know that a source file can't be precompiled.
And about that:
Originally Posted by itsmeandnobodyelse
(what means an additional .cpp that creates the pch)
In wxDev-C++ I can't create a .pch from a .cpp file...
-
August 7th, 2010, 09:29 AM
#15
Re: Why don't set every header file as precompiled ?
Now, I know why
According to the wxWidgets Wiki:
Originally Posted by wxWidgets Wiki
(Annotation: The wxDevCpp IDE grants you the freedom to define any c, cpp or header file to "create" the precompiled header. This is bullshit. It actually DOES use this setting only when it is set on a header file. All other files are automatically set to "use" the precompiled header anyway.)
Originally Posted by itsmeandnobodyelse
all statements added above include of precompiled headers were ignored by the compiler.
Yes, I heard something about that... Do you know why are they ignored ?
Originally Posted by itsmeandnobodyelse
Depending on your 'include strategy' which normally should be minimal and complete [...] that could be an overhead cause normally a change on a subheader should only reflect on cpp's which were using that header.
Then if change a subheader, the normal main header won't need to be compiled again ?
If so, it doesn't seem to be logical as the main header will be changed too...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|