error Please #define _AFXDLL or do not use /MD[d] occurs even after making changes in Project Properties

Visual C++DllMfc

Visual C++ Problem Overview


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.

Visual C++ Solutions


Solution 1 - Visual C++

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"

Solution 2 - Visual C++

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

Solution 3 - Visual C++

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

Solution 4 - Visual C++

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.

Solution 5 - Visual C++

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

Solution 6 - Visual C++

  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.

Solution 7 - Visual C++

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

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

Solution 8 - Visual C++

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

Solution 9 - Visual C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSayaliKView Question on Stackoverflow
Solution 1 - Visual C++Ananthalakshmi SankarView Answer on Stackoverflow
Solution 2 - Visual C++Chris KennedyView Answer on Stackoverflow
Solution 3 - Visual C++ingcontiView Answer on Stackoverflow
Solution 4 - Visual C++Vojtěch FriedView Answer on Stackoverflow
Solution 5 - Visual C++AhmedView Answer on Stackoverflow
Solution 6 - Visual C++Kaushik DuttaView Answer on Stackoverflow
Solution 7 - Visual C++Sagar DeshpandeView Answer on Stackoverflow
Solution 8 - Visual C++Kevin S. MillerView Answer on Stackoverflow
Solution 9 - Visual C++Blake MitchellView Answer on Stackoverflow