peterchen
peterchen

Reputation: 41096

How to detect "Use MFC" in preprocessor

For a static Win32 library, how can I detect that any of the "Use MFC" options is set?

i.e.

#ifdef ---BuildingForMFC---
....
#else
...
#endif

Upvotes: 13

Views: 5010

Answers (3)

Mouze
Mouze

Reputation: 706

I've check on Visual Studio 2013, in an original project targeting only Win32 console, so I've to add MFC support (without using of the Project Wizard) in a second time. Following are my findings:

  • The macro _MFC_VER is defined in the afxver_.h, included by afx.h. So, if you don't include afx.h directly/indirectly in your .cpp file, you don't have the _MFC_VER macro defined. For example, including in a project a source .cpp that doesn't include the afx.h, the file will be compiled WITHOUT the definition of _MFC_VER macro. So it's useless for adapt the c++ code (an external library, for example) to detect usage of MFC library and optionally support the MFC library.

  • If you manually turn on the use of MFC (Select the project in Solution Explorer, than right click, Configuration Properties -> General -> Use of MFC) you have two possibilities:

    • A) select "Use MFC in a Shared DLL" option. This actually update the command line parameters adding the definition of _AFXDLL to the preprocessor macro list.
    • B) select "Use MFC in a Static Library" options. This actually remove the _AFXDLL macro defined, but no macro definition is added, so nothing can told you if MFC is actually used.

So, during my test activity, only the mode A can be used effectively to understand if the MFC library is included or not in the project under building.

I maintain a C++ cross-platform library that support many platforms (Mac OSx, WinX console, WinX MFC, iOS, Unix, Android) and enabling MFC with dynamic DLL is the only way to transparently detect the MFC presence. So for example:

#if defined(_AFXDLL)
#    include <afx.h>
#endif

Obviusly, you can add manually a macro definition (_AFX) in the project preprocessor list.

Upvotes: 5

Bruce Ikin
Bruce Ikin

Reputation: 905

I have always checked for the symbol _MFC_VER being defined.

This is the version number of MFC being used 0x0700 = 7.0

It is in the "Predefined Macros" in MSDN

Upvotes: 14

1800 INFORMATION
1800 INFORMATION

Reputation: 135245

The symbol _AFX is typically defined for MFC projects.

Upvotes: 2

Related Questions