预处理器`_AFXDLL`是什么意思?

2022-01-12 00:00:00 dll c++ mfc

我已经搜索但没有找到有意义的解释,比如效果是什么,机制,所以我可以理解它.一个有意义的解释"的例子,网上有相当多的资料解释_DEBUG预处理器,如何开启它会允许assert

I've searched but didn't hit a meaningful explanation, like what's the effect, the mechanism, so I could understand it. An example for "meaningful explanation", there are quite some materials online explaining _DEBUG preprocessor, how turn it on will allow assert etc.

MSDN 页面 #1 Building Settings for an MFC DLL for Visual Studio 6.0 表示这不适用于 1) 常规 DLL,静态链接到 MFCrequired,但需要 2) 常规 DLL,使用共享的 MFC DLL,或者,用于 3) 扩展 DLL.但没有进一步的解释.

MSDN page #1 Building Settings for an MFC DLL for Visual Studio 6.0 says this is not for 1) Regular DLL, statically linked to MFCrequired, but required for 2) Regular DLL, using the shared MFC DLL, or, for 3) Extension DLL. But there's no further explanation.

MSDN page #2 AFXDLL Versions 简要提及:

MSDN page #2 AFXDLL Versions briefly mentioned:

而不是通过静态链接到 MFC 来构建应用程序目标代码库,您可以构建您的应用程序以使用其中之一AFXDLL 库,其中包含多个运行的 DLL 中的 MFC应用程序可以共享.有关 AFXDLL 名称的表,请参阅 DLL:命名约定.

Instead of building your application by statically linking to the MFC object-code libraries, you can build your application to use one of the AFXDLL libraries, which contain MFC in a DLL that multiple running applications can share. For a table of AFXDLL names, see DLLs: Naming Conventions.

注意:默认情况下,MFC 应用程序向导会创建一个 AFXDLL项目.要改为使用 MFC 代码的静态链接,请将 Use MFC 设置为MFC 应用程序向导中的静态库选项.静态链接在 Visual C++ 标准版中不可用.

Note: By default, the MFC Application Wizard creates an AFXDLL project. To use static linking of MFC code instead, set the Use MFC in a static library option in the MFC Application Wizard. Static linking is not available in the Standard Edition of Visual C++.

,但我什至不确定这个 AFXDLL 是否与 _AFXDLL 预处理器有关.

, but I'm even not sure if this AFXDLL is related to _AFXDLL preprocessor.

Stackoverflow 发布 无法解释的错误请使用_AFXDLL 构建的/MD 开关" 和 #error请为 _AFXDLL 构建使用/MD 开关 说明 /MT/MTd_AFXDLL 冲突,但这是相同的作为 MSDN 第 1 页,没什么新意.

Stackoverflow post Unexplainable error "Please use the /MD switch for _AFXDLL builds" and #error Please use the /MD switch for _AFXDLL builds explained that /MT or /MTd conflict with _AFXDLL, but this is the same as the MSDN page #1, nothing new.

谁能解释一下

  • _AFXDLL 预处理器是什么意思?最初的动机?
  • 它对编译或链接有何影响?
  • 它已经过时了吗?
  • _AFXDLL 预处理器是否与AFXDLL 库相关?实际上,这个所谓的AFXDLL 库"是什么?
  • what does _AFXDLL preprocessor mean? the original motivation?
  • how does it affect the compiling or linking?
  • is it obsolete?
  • is _AFXDLL preprocessor related to the AFXDLL libraries? and actually, what is this so called "AFXDLL library"?

推荐答案

MSVC++ 为只想部署单个可执行文件的程序员提供了优化.您可以使用/MT 构建以将 C 运行时库和标准 C++ 库链接到 EXE.您可以链接静态 MFC 库以将 MFC 链接到 EXE.对于小型 LOB 应用来说并不少见.

MSVC++ provides an optimization for programmers that want to deploy only a single executable file. You can build with /MT to link the C runtime library and the standard C++ library into the EXE. And you can link the static MFC libraries to link MFC into the EXE. Not uncommon for small LOB apps.

很好,但是当您还使用 DLL 来模块化或共享您的代码或支持更快的构建时间时,这将无法正常工作.MFC(和 CRT)有很多全局状态,CWinApp 单例就是一个很好的例子.C++ 程序不像 Java 或 C# 应用程序那样在 VM 中运行,一个模块必须负责存储该全局状态.并且其他模块必须使用该模块的全局变量,而不是它们自己的.所以你必须使用 MFC 的 DLL 版本和 C 和 C++ 运行时库的 DLL 版本,并链接它们的导入库,他们承担了这个责任.

Nice, but that cannot work properly when you also use DLLs to modularize or share your code or favor faster build times. MFC (and the CRT) have lots of global state, the CWinApp singleton is a good example. A C++ program does not run in a VM like Java or C# apps do, one of the modules has to take responsibility to store that global state. And the other modules must use that module's globals, not their own. So you must use the DLL version of MFC and the DLL version of the C and C++ runtime libraries, and link their import libraries, they take on that responsibility.

你必须告诉编译器,这样库才能为这些全局变量寻找合适的位置.这需要#defining _AFXDLL.像应用程序框架存在于它自己的 DLL 中"一样阅读它.CRT 也有一个宏,它是 _DLL.

And you have to tell the compiler about it, so that the library will go looking for the right place for those globals. That requires #defining _AFXDLL. Read it like "the application framework lives in its own DLL". The CRT has a macro for that as well, it is _DLL.

请注意,这在 IDE 中是完全自动的.项目 > 属性 > 常规,使用 MFC"设置.如果您为此设置选择在共享 DLL 中使用 MFC",那么您还将自动获得 _AFXDLL 定义.CRT 也一样.

Do note that this is completely automatic in the IDE. Project > Properties > General, "Use of MFC" setting. If you pick "Use of MFC in a Shared DLL" for this setting then you automagically will also get _AFXDLL defined. Same for the CRT.

相关文章