2014-03-03 81 views
1

我创建简单的Win32控制台应用程序:预编译头和__AFXWIN_H__

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

+0

stdafx.h必须包含在.cpp文件中而不是.h文件中。 –

+0

它包含在我的应用程序cpp文件(第一个代码块) – vico

+0

它包含在你的h文件(第二个代码块)中。 –

回答

0

请勿在标题中包含外部标题或stdafx.h。

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

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

由于外部头文件通过PCH处理一次,所以这种方法的构建速度更快。

1

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

+1

但是,我真的需要调用使用MFC只从C++ MFC项目完成的dll吗? – vico

+0

取决于如何制作dll。你从它出口什么? – Loghorn