CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Do header files with implementation work in a precompiled header?

  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Do header files with implementation work in a precompiled header?

    Let's suppose we have this header file:

    People.h
    Code:
    class People
    {
        public:
        People(const std::string &name, int age)
            : m_name(name), m_age(age)
        {
        }
    
        inline const std::string& GetName() const { return m_name; }
        inline int GetAge() const { return m_age; }
    
        inline std::string ToString() const { std::stringstream ss; ss << m_name << " , " << m_age; return ss.str(); }
    
    
        private:
        std::string m_name;
        int m_age;
    };
    pch.h
    Code:
    #include "People.h"
    If you include a header file like that in a precompiled header, will it work!

    If you include that pch.h in a bunch of .cpp files, you basically include People.h too. So if I make changes to that .cpp files, wouldn't People.h be recompiled each time too?

    Also what happens if I have something like this:
    MyClass.h
    Code:
    #include "People.h"
    class MyClass
    {
        public:
        MyClass();
        
         //Allocate on the stack.
         People people("name", 20);
    }
    MyClass.cpp
    Code:
    #include "pch.h"
    #include "MyClass.h"
    
    //implementation of MyClass
    Will the above recompile People.h every time I make changes?
    Last edited by babaliaris; September 11th, 2020 at 06:26 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,319

    Re: Do header files with implementation work in a precompiled header?

    Pre-compiled headers are for header files that don't change very much. If you are still making changes to pre-compiled header files, don't include them for compiled headers - just use them as normal. Usually header files for pre-compilation are placed in stdafx.h - but this file name can be changed in project/properties/c++/precompiled headers/precompiled header file. Header files not included within this specified header file are not pre-compiled.

    If a change has occurred in a pre-compiled header (and its dependencies) since the last pre-compile, then it is re-compiled.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.8.2)

  3. #3
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Re: Do header files with implementation work in a precompiled header?

    Quote Originally Posted by 2kaud View Post
    Pre-compiled headers are for header files that don't change very much.
    Yes, I know that! The thing is that I'm using some quite big header-only libraries and every time I change my code it takes 1 minute to compile.
    And I'm talking about the beginning of a project where I literally don't have more than 100 lines of code, when I change my own CPP files all these header-only libraries are
    recompiled over again... And these libraries don't change until I do a git pull (if the contributors have made changes to the master branch).

    Also every time I'm making a single change (for example changing the value of an integer) and re-run my application to test it, it takes too much time... And this is really annoying when debugging.

    So in this case, is a precompiled header what I'm looking for? I thought a precompiled header works only with header files which do not include implementations.



    Quote Originally Posted by 2kaud View Post
    If a change has occurred in a pre-compiled header (and its dependencies) since the last pre-compile, then it is re-compiled.
    So, I don't have to do a clean build every time I change a dependency of the precompiled header?
    Last edited by babaliaris; September 11th, 2020 at 10:49 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,319

    Re: Do header files with implementation work in a precompiled header?

    Have you got the headers that you want to be pre-compiled included in the right header file set up for pre-compilation? What option for pre-compilation are you using - create or use? Don't put these headers you want to be pre-compiled in your program. Put them in say stdafx.h, tell the compiler this is the pre-compiled header file, and in say stdafx.h include the headers you want to be pre-compiled.

    So myclass.cpp would be:

    Code:
    #include "stdafx.h"
    and stdafx.h would be:

    Code:
    #include "pch.h"
    #include "MyClass.h"
    // other include here for pre-compiled
    and the compiler properties would have stdafx.h set as the pre-compiled header file. Note there is only 1 pre-compiled header file per project!


    There shouldn't be a need to do a re-build solution - incremental build should work. Sometimes though it gets 'confused' and a re-build is required to sort it out - but this should be the exception.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.8.2)

  5. #5
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    127

    Re: Do header files with implementation work in a precompiled header?

    I have the option Use(/Yu) in the precompiled header options.
    Precompiled header file : pch.h
    Precompiled header source file : <full_path_to>pch.cpp

    In the include direcotries of the project, I point the root directory where the pch.h lives.

    In every cpp file, I include the pch.h (first line in the file).

    By the way, I haven't test it. In the previous post that I said about my projects with these big header-only libraries I was running the project without a precompiled header.
    Last edited by babaliaris; September 11th, 2020 at 12:45 PM.

  6. #6
    Join Date
    Feb 2017
    Posts
    579

    Re: Do header files with implementation work in a precompiled header?

    Quote Originally Posted by babaliaris View Post
    I'm using some quite big header-only libraries and every time I change my code it takes 1 minute to compile.
    And I'm talking about the beginning of a project where I literally don't have more than 100 lines of code, when I change my own CPP files all these header-only libraries are
    recompiled over again...
    You could write your own interface to the big header-only libraries with the implementation part of the interface on one or a few .cpp files. If you don't change the interface then the .cpp files won't re-compile. The interface has the additional benefit of isolating your code from the external libraries.

    I have a medium size VS-project consisting of .h files only and just one .cpp file holding the entry point main(). The header pre-compilation is switched off. In spite of this recompilation is very fast indeed. It seems to me that the header pre-compilation option is kind of obsolete. If VS studio is given free rein by only having to deal with .h files it does a remarkable job of keeping recompilation at a minimum. I've noticed that it keeps track of the individual dependencies of every single function and method. And if push comes to shove there's always the option of introducing .cpp files selectively to enforce pre-compilation (as I suggested above) but I've never felt a need for it.

    In the future, hopefully modules in C++ 20 will put an end to this kind of problems.
    Last edited by wolle; September 13th, 2020 at 12:43 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)