c++visual-studioprecompiled-headers

Using Precompiled header within my library project


I have been studying Game engine project and at some point, the guide introduces precompiled header file.

So my pch.h includes some of frequently used C++ standard library, STL and my almost non-changing headers like logger class headers (done implementing it).

My question is like this.

pch.h (in my game engine library project)

#pragma once

#include <string>
#include <vector>
#include <Windows.h>
// and more...

App.h (in my game engine library project)

#pragma once

// I usually include stuffs even though they might be included already from other header files
// I like to this "included these because I use them in my App class!"
// and they will be excluded even if they are duplicates thanks to include guards anyway
// 
// But commented them out since I am using pch 
// and intellisense works well without explicitly including them
// #include <string>
// #include <vector>

class App {
// some class stuff...

private:
    std::string      m_String;
    std::vector<int> m_Vector;
};

So I have <string> and <vector> included in my pch. What happens when compiling my library project? Is it correct that since <string>, <vector> are already in pch, compiler can skip compiling them by using info already in pch?

I have been struggling with this because in my other project which acts like client, links to my game engine library had compilation errors. Apparently I had to include standard library headers like App.h above to stop compilation error caused by not including the headers.(I mean commentting out <string>, <vector> in App.h above


I have some kind of answer after struggling hours, but please feel free to share your opinions.


Solution

  • Precompiled headers and normal includes

    In fact, it's good practice to continue using your includes as if you never had precompiled headers on in the first place. This helps in case you turn off precompiled headers in the future or modify the list of headers in it, or someone else decides to do their own out-of-source build that doesn't use PCH.

    After struggling for hours, I found this answer. It seems like I still need to include standard library headers in pch normally according to this answer.

    My guess is I just keep including those <string>, <vector> normally in project's header files so that clients can properly use my engine project.(Use my header files in engine's include/ folder)

    MSVC will only save time building my engine.lib whenever MSVC finds header files that are already in pch.h during compilation.