在 stdafx.h 中声明 <afxdb.h> 时出错

error in declaring <afxdb.h> inside stdafx.h

本文关键字:gt 出错 afxdb stdafx 声明 lt      更新时间:2023-10-16

我正在尝试使用CDatabase,为此我在stdafx.h文件中声明了<afxdb.h>。当我编译此代码时,我得到一个错误"WINDOWS.H已经包含。MFC应用程序必须不是#include <windows.h>"。为什么会发生这种情况?这不是使用CDatabase的正确头文件吗?这是默认生成的代码。。。

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers

// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

如果在#include <tchar.h>之后添加#include <afxdb.h>,则会得到指定的错误。

谢谢

只需添加以下行:

#define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
#include <afxwin.h>   <--
#include <afxext.h>   <--
#include <afxdb.h>    <--
// Windows Header Files:
#include <windows.h>

但是,由于您以前没有在该项目中使用MFC,您可能需要将项目属性->常规->MFC的使用从Use Standard Windows Libraries更改为Use MFC in a Static Library,这样链接器就可以满足要求,并且不会因未解析的外部符号错误而困扰您。

希望这能有所帮助;)

您需要首先#包含afx标头。之后,您可能仍然是#include <windows.h>,尽管这不是必需的。因此,只要更改包含的顺序,它就会起作用。