56

I am working on Win32 project in Visual Studio 2011. It is generating MFC error when I includes afx.h or afxwin.h. To resolve this, I have made the following changes in the Project Properties tab : 1) Use of MFC : Use MFC in a shared DLL 2) C++ -> Code Generation -> Runtime Library -> Multi-threaded Debug DLL(/MDd)

Still it gives me following error when I build the solution :

1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\vc\atlmfc\include\afx.h(24): fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]

My question is why Win32 project is generating MFC error and how should I remove this error.Kindly guide me.

SayaliK
  • 563
  • 1
  • 4
  • 6
  • 3
    Why don't you just do what it says and #define _AFXDLL? C/C++, Preprocessor, Preprocessor Definitions setting. – Hans Passant Jul 30 '14 at 07:36
  • 1
    Look at the build log file - make sure there's a `/D _AFXDLL` in the command line issued for the compile. The "Use MFC in a shared DLL" should cause that to happen. Maybe a clean/rebuild might help? Also, VC++11 is Visual Studio 2012. – Michael Burr Jul 30 '14 at 07:37
  • Thanks Hans Passant. I have added _AFXDLL in the Preprocessor definitions. But it still gives the same error. – SayaliK Jul 30 '14 at 08:20
  • What is the project type? (dll? static lib? exe?) – Ofek Shilon Jul 30 '14 at 08:43
  • Check what Michael said: in the build log, is there a _AFXDLL? Do not define it manually, it is added when you set "Use MFC in a shared DLL". If it is set there, this sounds like a stale preprocessed header thing. Manually delete everything that is not a source file, project file or resource and build again. – Roel Jul 31 '14 at 07:54
  • The project on which I am working has its own method of building a solution. So I am not able to check build log. – SayaliK Jul 31 '14 at 09:00

9 Answers9

54

On Visual Studio 2011, this worked for me:

Project -> "project" Properties -> Configuration Properties -> General -> Project Defaults -> Use of MFC : Use MFC in a shared DLL

(In Visual Studio 2019, the latter setting can be found in "Properties -> Configuration Properties -> Advanced -> Use of MFC"

  • 6
    Can anyone explain me why is this answer upvoted and the other one not? "Use MFC in a shared DLL" adds /D "_AFXDLL" so it does exactly the same in a more cryptic (microsoft) way. And I have no clue how does /showIncudes relate to the issue... – Maciek Feb 23 '16 at 04:18
  • 3
    It is not necessary to add the /showIncludes flag, but thanks for pointing out the Use of MFC setting. – Bondolin Jul 13 '16 at 14:30
  • 1
    Additionally, the author of the post said that he already set the project properties to use MFC in a shared DLL but it did not resolve the error. Frankly, none of the answers actually answer the original question. – shawn1874 Aug 23 '17 at 20:22
  • /showIncludes will print all of the files being included. Source: https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-alphabetically?view=vs-2019 – Seabass77 May 09 '19 at 17:04
  • Thank you for pointing out that /showIncludes was unnecessary to resolve this. – Ananthalakshmi Sankar Aug 26 '21 at 06:39
16

I had the same problem, but only solved it when I realized I had to set the "Use MFC in a shared DLL" flag for both debug and release configurations (I had only set it for debug).

Chris Kennedy
  • 167
  • 1
  • 7
14

In my experience is a two ways step. suppose You want STATIC linking: a) set "Use MFC in a Static Library" b) add: #define _AFXDLL 1 in stdafx.h

works on VS 2012

ingconti
  • 10,876
  • 3
  • 61
  • 48
4

I struggled with a similar problem. In my case it was caused by the settings that were attached to individual cpp files. Since they contained preprocessor symbols, they actually blocked the project (or props) level settings that utilize preprocessor symbols.

So check if you have file level settings in your vcxproj. If you do, check if they are equal to the project level settings. If they are, you can safely remove them. If there are differences, you have to sort that out.

Cpp file level settings should usually be empty (there are some exceptions when you need them), but it is quite easy to add them by accident.

Vojtěch Fried
  • 111
  • 2
  • 5
2

I was getting this error because I did not properly set the configuration of the project to "Use MFC in a Shared DLL". My mistake was that I set this option only for Release mode and when I compiled in Debug mode I got this error. Applying the settings for both the Debug & Release mode configuration solved the problem for me.

Following was the settings:

Project -> "project" Properties -> Configuration Properties -> General -> Project Defaults -> Use of MFC :Use MFC in a shared DLL

Ahmed
  • 489
  • 4
  • 12
2
  1. Open

    Project Properties -> Configuration Properties -> C/C++ -> Code Generation -> Runtime Library

  2. Select Multi-threaded (/MT) in Runtime Library Section.

This will remove your error. Enjoy.

RobC
  • 22,977
  • 20
  • 73
  • 80
1

I have also faced the same error. The error message is self explanatory.

You need to add #define _AFXDLL in your AFX.h file.

RobC
  • 22,977
  • 20
  • 73
  • 80
0

For me, the target had to be changed from x64 to x86.

Kevin S. Miller
  • 913
  • 1
  • 9
  • 21
0

I have also had this problem using vs2019. For me, a better way to state the path, I think, is: project -> properties -> configuration (which opens automatically after clicking on properties) and then -> advanced -> mfc. It really threw me for a while when I clicked on properties and then could not find "configuration properties" until I noticed the advanced in the list already on the screen.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Miguel Jan 12 '22 at 20:37