55

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.

6
  • 3
    Why don't you just do what it says and #define _AFXDLL? C/C++, Preprocessor, Preprocessor Definitions setting. Jul 30, 2014 at 7: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. Jul 30, 2014 at 7:37
  • Thanks Hans Passant. I have added _AFXDLL in the Preprocessor definitions. But it still gives the same error.
    – SayaliK
    Jul 30, 2014 at 8:20
  • What is the project type? (dll? static lib? exe?) Jul 30, 2014 at 8: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, 2014 at 7:54

9 Answers 9

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"

5
  • 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, 2016 at 4:18
  • 3
    It is not necessary to add the /showIncludes flag, but thanks for pointing out the Use of MFC setting.
    – Bondolin
    Jul 13, 2016 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, 2017 at 20:22
  • /showIncludes will print all of the files being included. Source: learn.microsoft.com/en-us/cpp/build/reference/…
    – Seabass77
    May 9, 2019 at 17:04
  • Thank you for pointing out that /showIncludes was unnecessary to resolve this. Aug 26, 2021 at 6: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).

0
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

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.

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

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.

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.

0

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

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.

1
  • 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.
    – Miguel
    Jan 12, 2022 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.