CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2009
    Posts
    28

    Question 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.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Why don't set every header file as precompiled ?

    inline functions can not be precompiled. template functions can not be precompiled.

  3. #3
    Join Date
    Jul 2002
    Posts
    2,543

    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.

  4. #4
    Join Date
    Jul 2009
    Posts
    28

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Alex F View Post
    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...

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile 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.

    Quote Originally Posted by Quentin026 View Post
    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

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Why don't set every header file as precompiled ?

    Just FYI, wxDev-C++ is no longer supported, it got merged with Code::Blocks.

  7. #7
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Quentin026 View Post
    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

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Quentin026 View Post
    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.

  9. #9
    Join Date
    Jul 2009
    Posts
    28

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by itsmeandnobodyelse View Post
    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.

    Quote Originally Posted by itsmeandnobodyelse View Post
    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...

  10. #10
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Quentin026 View Post
    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.

  11. #11
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Quentin026 View Post
    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

  12. #12
    Join Date
    Jul 2009
    Posts
    28

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by ninja9578 View Post
    Yes, but then your header may not have inline function, template functions, or macros.
    Then why can a precompiled .cpp file contain those things ?

  13. #13
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Quentin026 View Post
    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

  14. #14
    Join Date
    Jul 2009
    Posts
    28

    Re: Why don't set every header file as precompiled ?

    Quote Originally Posted by Quentin026 View Post
    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:

    Quote Originally Posted by itsmeandnobodyelse View Post
    (what means an additional .cpp that creates the pch)
    In wxDev-C++ I can't create a .pch from a .cpp file...

  15. #15
    Join Date
    Jul 2009
    Posts
    28

    Re: Why don't set every header file as precompiled ?

    Now, I know why

    According to the wxWidgets Wiki:

    Quote 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.)
    Quote Originally Posted by itsmeandnobodyelse View Post
    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 ?

    Quote Originally Posted by itsmeandnobodyelse View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured