c++ - 使用 .def 文件的优缺点

标签 c++ windows dll shared-libraries declspec

我不明白这一段:

Exporting functions in a .def file gives you control over the export ordinals. When you add an exported function to your DLL, you can assign it a higher ordinal value than any other exported function. When you do this, applications that use implicit linking do not have to relink with the import library that contains the new function. This is very convenient if you are designing a DLL for use by many applications because you can add new functionality and also ensure that it continues to work correctly with the applications that already rely on it. For example, the MFC DLLs are built by using .def files.

为什么在将函数添加到 dll 的情况下使用 .def 文件而不是 __declspec(dllexport) 时应用程序不必重新链接导入库?

比照https://learn.microsoft.com/en-us/cpp/build/determining-which-exporting-method-to-use

最佳答案

这是因为共享对象(或 DLL)的 MSFT 实现的一些细节。在 Microsoft 世界中,为了将函数导入到您的进程中,您不仅需要共享代码本身 (.dll),还需要特殊的“导入”库 - .lib 文件。该文件静态链接到您的应用程序(因为它是静态库)。该库在函数名称和函数序号之间提供“粘合剂”。

通常,每次发布新版本的 DLL 时,所有使用它的应用程序都必须与新的随附版本的静态导入库 (.lib) 重新链接,以便能够使用这个新的 DLL 库。这是因为创建新库后函数序号通常不再有效。但是,如果您使用的是 .def 文件,您可以手动分配序号,并确保序号对于以前可用的函数保持不变 - 因此 .lib 文件仍将是有效。

关于c++ - 使用 .def 文件的优缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49157641/

相关文章:

c++ - boost::ref 是如何工作的?

c++ - 从其他线程关闭对话框后无法从主窗口聚焦

windows - 正在搜索命令提示符参数?

c++ - 在 C++ 中从 dll 调用 sqrt。访问冲突

c++ - DLL缓存问题

c++ - 在哪里可以找到有关如何解析 C++ 头文件的良好说明?

c++ - 奇怪的 C++ 异常 "definition"

python - python /畅达/ python /Windows : how to start and activate a conda environment?

windows - C++11 + SDL2 + Windows : Multithreaded program hangs after any input event

c - DLL:在 Linux for Windows 上使用 MinGW 编译 C 时无法识别文件格式