是否有在多个项目中使用 pre-compiled headers 的最佳方式?

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

我有一个包含 2 个项目的解决方案,一个是静态库,另一个是

链接到它。在静态库中,我有一个 pre-compiled header 文件,其中包含以下代码:

#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>

我还添加了必要的 .cpp 文件并配置了项目属性以使用此特定的预

编译header。我还根据需要将 .h 文件添加到每个 .cpp 文件的顶部。我继续我的

后一个项目并正确引用了静态库并编写了一些简单的代码,这是一个

示例:

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

    }

    ~Test()
    {

    }
};

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

此返回的测试 object 将与入口点链接并通过管道继续进行

遇到来自 std 库的代码,那时我得到这些错误:

我了解到此项目无法识别我的 pre-compiled header 中的 header 文件。我可以

确认我将这些文件包含在应用程序中并且这解决了所有错误。这引发了许多

问题:这个项目链接到库,为什么它不能识别这个 pre-compiled

header?最好的解决方案是什么?每个项目是 pre-compiled header 吗?有什么

完全不同?

解决方案是简单地将您的 pch 包含在每个源文件中,并添加头文件使用的任何必要的头文件。编译器仍将使用 pch,但也可以在头文件中定义您的函数。