visual-studio-2019pch

Is there an optimal way to use pre-compiled headers in multiple projects?


I have a solution with 2 projects, one is a static library and the other is an application that

links to it. In the static library I have a pre-compiled header file with the following code:

#pragma once

//C standard Library
#include <stdio.h>

//C++ Standard Library
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <functional>

//Data Structures
#include <string>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

I have also added the necessary .cpp file and configured the project properties to use this specific pre-

compiled header. I also added the .h file to the top of every .cpp file as required. I proceeded to my

latter project and properly referenced the static library and wrote some simple code, here is an

example:

class Test : public Craft::Application
{
public:
    Test()
    {

    }

    ~Test()
    {

    }
};

Craft::Application* Craft::CreateApplication()
{
    return new Test;
} 

This returned Test object will be linked with an entry point and proceeds through the pipeline and

encounters code from the std library, that's when I get these errors:

I understand that this project doesn't recognize the header files in my pre-compiled header. I can

confirm that as I included these files in the application and this resolved all errors. This sparks many

questions though: This project links to the library, so why doesn't it recognize this pre-compiled

header? What's the best solution for this? Is it a pre-compiled header per project? Is it something

entirely else?


Solution

  • The solution is simply including your pch in every source file and adding any necessary headers used by the header file. The compiler will still use the pch but will also be able to define your functions in the header file.