Construction of a .dll file and the intermediate .lib

时间:2015-10-29 15:51:16

标签: c++ visual-studio dynamic static-libraries import-libraries

Below is an excerpt from link1.

Microsoft introduced __export in the 16-bit compiler version of Visual C++ to allow the compiler to generate the export names automatically and place them in a .lib file. This .lib file can then be used just like a static .lib to link with a DLL. In newer compiler versions, you can export data, functions, classes, or class member functions from a DLL using the __declspec(dllexport) keyword. __declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.

I understand the above paragraph to an extent but not very well.

Below is an excerpt from link2.

When building the DLL, the linker uses the .def file to create an export (.exp) file and an import library (.lib) file. The linker then uses the export file to build the DLL file. Executables that implicitly link to the DLL link to the import library when they are built.

Now, this makes me confused and made me ask the below questions:

  1. Could anybody, in simple words, tell me what the term exporting really means? I believe this is making an object accessible from one piece of the code to other - but hey !!

  2. When building projects with old libraries, I see .def file in majority of them. But the latest compilers automatically exports objects. Would the presence of the .def file cause any conflict when converting a older version visual studio project to the newer one?

  3. What is the use of the .lib(the so called import file) after the generation of the .dll. Can it be safely deleted?

  4. ARRGGGH !! What is the difference between a static library(.lib) and import library(.lib)? Blunder, huh? But still !!

  5. Is the windows specific phenomenon? I believe it is not. What is the Linux counterpart of the so called import file?

Please feel free to rephrase the question if it is not already lucid.

1 个答案:

答案 0 :(得分:2)

  

告诉我出口的真正含义是什么?

它只是意味着告诉链接器需要将一个条目放入DLL的导出表中。操作系统加载程序稍后使用它在运行时将不同模块中的代码粘合在一起。

  

我在大多数人看到.def文件

可能非常旧项目。或者它从未作为一个旨在创建单独模块的项目启动。像静态库一样,源代码没有__declspec属性。跨平台库很可能适合该法案。 C和C ++语言规范仍无法以标准化方式创建模块。每个人都这样做,没有人这样做。大量的时间消耗。

  

.lib(所谓的导入文件)的用途是什么

项目中使用 DLL是必要的。链接器需要知道标识符存在于另一个构建中,并且无法在链接时解析。它将一个条目放在操作系统加载器使用的另一个表中,即导入表。它是一个 very 简单文件,它只列出导出标识符的名称。从理论上讲,链接器可以使用DLL本身来解决这个问题。实际上这不起作用,因为导出的名称不必与实际名称匹配。

  

静态库(.lib)和导入库(.lib)

之间有什么区别

静态库包含链接到使用该库的项目的代码。导入库不包含代码,只是提示代码在其他地方可用。

  

窗户是否有特定现象?

粗略地说,是的。 Unix具有相同的概念,但实现方式却截然不同。