0

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

  • Can give some example code? Looks like you try to load several times you header files. Have a look at http://stackoverflow.com/q/2939368/1141095 – ezdazuzena Feb 06 '12 at 09:47
  • Maybe a bit more organized? You are allowed to change you question and put the code there. Have in mind that people might only have the information, that you provide them with. – ezdazuzena Feb 06 '12 at 10:00

2 Answers2

0

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.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • Great.. this works. But I get some weird errors after I declare a CDatabase object. **1:)unresolved external symbol __endthreadex** **2:)unresolved external symbol __beginthreadex** any idea what that might be? –  Feb 06 '12 at 10:39
0

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 ;)

LihO
  • 41,190
  • 11
  • 99
  • 167
  • Great.. this works. But I get some weird errors after I declare a CDatabase object. **1:)unresolved external symbol __endthreadex 2:)unresolved external symbol __beginthreadex** any idea what that might be? –  Feb 06 '12 at 10:43
  • Project properties -> General -> Use of MFC: change `Use Standard Windows Libraries` to `Use MFC in a Static Library` – LihO Feb 06 '12 at 10:45
  • thank you very much. It works. If you don't mind, can you please explain me how this change affects it? I am new to this –  Feb 06 '12 at 10:49
  • MFC is a **library** which means you work with it just like you work with any other library. Due to including of afx*.h headers you know the interface and Intellisense of Visual Studio is satisfied since it has declarations of these methods. But their implementations are not available if you don't provide info about library files. – LihO Feb 06 '12 at 11:40