error in declaring <afxdb.h> inside stdafx.h
-
25-04-2021 - |
Pergunta
I am trying to use CDatabase and for that I have declared <afxdb.h>
in stdafx.h file.
When I compile this code I get an error "WINDOWS.H already included. MFC apps must not #include <windows.h>
". Why does this happen? Isn't this the right header file to use CDatabase?
This is the default generated code...
#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>
If I add #include <afxdb.h>
after #include <tchar.h
> I get the error specified.
Thank You
Solução
Just add these lines:
#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>
But since you didn't use MFC in this project before, you will probably have to change Project properties -> General -> Use of MFC from Use Standard Windows Libraries
to Use MFC in a Static Library
so that linker is satisfied and doesn't bother you with unresolved external symbol errors.
Hope this helps ;)
Outras dicas
You need to #include afx-headers first. After that you might still #include <windows.h>
, although this is not necessary. So just change the order of your includes and it will work.