2014-03-03 83 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