预编译头和__AFXWIN_H__

时间:2014-03-03 13:17:13

标签: c++ visual-studio visual-c++

我创建了简单的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文件。

我已经包含了my.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'。我已经测试了使用/不使用预编译头的两个选项 - 相同的结果。问题在哪里?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

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

由于外部标题通过PCH处理一次,因此构建速度也会更快。