预编译标头和__AFXWIN_H__

Precompile header and __AFXWIN_H__

本文关键字:AFXWIN 编译      更新时间:2023-10-16

我创建了简单的win32控制台应用程序:

#include "stdafx.h"
#include <iostream>
#include "conio.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World" << endl;
    int num;
    cin >> num;
    return 0;
}

它编译得很好。

然后我正在尝试添加库。我有 dll 库和 .h 文件。

我已经包含了我的.h文件:

#include "stdafx.h"
#include "my.h"
#include <iostream>
#include "conio.h"

"my.h"包含以下行:

#ifndef __AFXWIN_H__
    #error include 'stdafx.h' before including this file for PCH
#endif

编译后出现错误:

 include 'stdafx.h' before including this file for PCH

但是我已经包含了"stdafx.h"。我已经使用使用/不使用预编译标头的两个选项进行了测试 - 相同的结果。 问题出在哪里?

您的my.h正在使用 MFC(__AFXWIN_H__由 MFC 标头定义),但您的控制台程序不是。必须在程序中使用 MFC 或重写库以不使用 MFC。

不要在标头中包含外部标头或 stdafx.h。

在 stdafx.h 中包含所有外部标头。

包括项目中每个 cpp 文件中的 stdafx.h。

这也将更快地构建,因为外部标头通过 PCH 处理一次。