2

I am trying to build and run an old (and quite large (approx. 100+ source and header files)) VS project. It was originally compiled using VS2012 and Windows 7, so I am using the same set-up (in fact, both Win 7 and 10). I am new to VS, so please bear with me. My problem is that that it fails to build with this fatal error:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxv_w32.h(16):
fatal error C1189: #error :  WINDOWS.H already included.
MFC apps must not #include <windows.h> (utils.cpp)```

I found that many people have had this problem. For example,

However, I could not find a solution that worked for me. First of all, as far as I am aware, windows.h is not included by our written code, but rather by the afxv_w32.h file. I have a utils.h file that hosts useful function prototypes, which includes a stdafx.h file (below). Through "Show includes" option I found that stdafx.h includes afxwin.h, which ends up with an inclusion of afxv_w32.h with the mentioned error; however, it seems the error is not raised at all times. For example:

1>  Note: including file: i:\hmcdeliverables\sourcecode\processor\utils.h
1>  Note: including file:  i:\hmcdeliverables\sourcecode\processor\stdafx.h
1>  Note: including file:   C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxwin.h
1>  Note: including file:    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afx.h
1>  Note: including file:     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\new.h
1>  Note: including file:      C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h
1>  Note: including file:     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxver_.h
1>  Note: including file:      C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxv_w32.h
1>  Note: including file:       C:\Program Files (x86)\Windows Kits\8.0\Include\um\winsdkver.h
1>  Note: including file:        C:\Program Files (x86)\Windows Kits\8.0\Include\shared\winapifamily.h
1>  Note: including file:       C:\Program Files (x86)\Windows Kits\8.0\Include\shared\sdkddkver.h
1>  Note: including file:       C:\Program Files (x86)\Windows Kits\8.0\Include\um\windows.h
1>  Note: including file:       C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\excpt.h
1>  Note: including file:         C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h
.
.    Some lines later...
.

1>  Note: including file: i:\hmcdeliverables\sourcecode\processor\utils.h
1>  Note: including file:  i:\hmcdeliverables\sourcecode\processor\stdafx.h
1>  Note: including file:   C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxwin.h
1>  Note: including file:    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afx.h
1>  Note: including file:     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\new.h
1>  Note: including file:      C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h
1>  Note: including file:     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxver_.h
1>  Note: including file:      C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxv_w32.h
1>  C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include\afxv_w32.h(16): fatal error C1189: #error :  WINDOWS.H already included.  MFC apps must not #include <windows.h>

utils.h is being used in many files of this project; however, I use the guard

#pragma once

to process it only once (if I understood this correctly). A snippet from the "utils.h":

#pragma once

#include "stdafx.h"
#include <string>
#include <opencv2/opencv.hpp>
#include <cstdint>

My stdafx.h file:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently

#pragma once

#ifndef _SECURE_ATL
#define _SECURE_ATL 1
#endif

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers
#endif

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER              // Allow use of features specific to Windows XP or later.
#define WINVER 0x0501       // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT        // Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif                      

#ifndef _WIN32_WINDOWS      // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE           // Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600    // Change this to the appropriate value to target other versions of IE.
#endif

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS  // some CString constructors will be explicit

// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions


#include <afxdisp.h>        // MFC Automation classes



#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>         // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#ifdef _UNICODE
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#endif

Where should I check or change to resolve this issue? Let me know if more info is required.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • 2
    stdafx.h is the precompiled header file. It must be #included in the .cpp file that takes advantage of it, and be the very first #include, never by a .h file. Hard to guess how this could have built properly before, but we can't the #includes used by utils.cpp. The file that it is actually complaining about. – Hans Passant May 08 '19 at 16:31
  • 2
    unrelated: Visual Studio 2012 is outdated by 5 years now. Consider upgrading to Visual Sudio 2017 or 2019. The Community Edition is free of charge. – Swordfish May 08 '19 at 16:39
  • I'd consider using APIs that are windows 7 and newer... but that's me – Mgetz May 08 '19 at 17:43
  • @HansPassant if I understood you correctly, I shouldn't include the precompiled header file in a header file (.h), but rather #include it only in the source (.cpp) files? Also, what did you mean by '..., never by a .h file'? – Gytis Bernotas May 08 '19 at 21:10

0 Answers0