86

During compilation and linking, what is use of .exp? What is the difference between .lib and .dll? I know that .lib will be used, while linking and .dll will be used when running the program. But what exactly is the difference between .lib and .dll?

Does .lib file not contain the code for the functions coming from .dll files? What is the need for using two separate files?

Please clarify.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Vineel Kumar Reddy
  • 4,588
  • 9
  • 33
  • 37

1 Answers1

119

In the case of an import library for a DLL, the .lib file does not contain any actual code at all. It basically contains just a list of the functions in the associated DLL -- enough for the linker to embed a reference to that DLL into something linked with the library, but not much else.

A .exp file is an export file -- basically just about the same as a .lib file. It's used (at least primarily) when you have a circular dependency. For example, assume you have a DLL that acts as a plug-in for an executable. The executable supplies some exported functions for use by plug-in DLLs, but also needs to be able to call some functions in the plug-ins as well (e.g. to load and initialize a plug-in).

The DLL won't link until the executable is built to provide a .lib file -- but the executable won't link until the DLL is built to provide a .lib file. To break the dependency, you run the linker against the executable, which fails (because it can't find a .lib file for the DLL), but will produce a .exp file. You then link the DLL against the .exp file for the executable. You can then re-run link to produce the executable, using the .lib file for the DLL.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • -1: Very old answer probably needs an update but surprising it has no more comprehensive answer. -1 as ny not even mentioning static vs dynamic library linkage you have incorrectly implied that library files do not contains function code (they clearly do as they are a collection of obj files - the op made no restriction to dynamic linkage only and potentially doesn't even know the difference. – David Apr 14 '23 at 06:18
  • 1
    @David: .exp files are used exclusively in conjunction with dynamic linking. so by asking about them, the OP most certainly did make a restriction to discussion of dynamic linking. I specifically said: "**In the case of an import library for a DLL** [...]" How do you believe that is "not even mentioning static vs. dynamic library linkage"? You do realize that "DLL" stands for "Dynamic Link Library", right? This answers what he was asking about, and nothing about the subject matter has changed significantly since. – Jerry Coffin Apr 14 '23 at 07:31
  • I agree your answer was appropriately qualified. It was only a -1 because the question never restricted it to the dynamic situation. Perhaps you might feel it a bit harsh - as obviously an expert would be aware that an exp file would only be produced when compiling a dll. However the op did not appear expert and also asked what is the difference between .lib and .dll so I'm not sure a novice would realise that .lib *can* contain code. Should I have written "(static vs. dynamic) library linkage" to make it clearer? – David Apr 16 '23 at 20:09