octopod
octopod

Reputation: 884

Are there different types of .lib files?

I've compiled a minimal example of code using Qt, and noticed that linking to its .lib files added a requirement for my compiled program to link to its corresponding .dll file.

I want to create a .lib myself for one of my other projects to use, but want to do so without having to also make a .dll for it to have to link to.


From the answer to this question: Difference between static and shared libraries?

Static libraries are .a (or in Windows .lib) files. All the code relating to the library is in this file, and it is directly linked into the program at compile time. A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. [Windows also has .lib files which are used to reference .dll files, but they act the same way as the first one].

Am I correct in understanding that there are two types of .lib files:

If this observation is correct, how would one go about compiling a .lib of one of these types?

Upvotes: 4

Views: 4450

Answers (1)

Yes, in this sense, there are two types of .lib files. This is specific to Windows (or, more exactly, to DLLs).

On Windows, a static library is a single file, normally with the extension .lib. Linking against a static library copies the code (object files) stored in it into your executable. This is equivalent to .a files of the Unix world.

A DLL (a shared library), on the other hand, has two parts: the dynamically-loaded library itself (.dll) which contains the code, and an import library (.lib) which contains sort of "stub code" for satisfying linker dependencies. You link against an import library (the .lib files accompanying the DLL), and that includes the "stub code" for the DLL's functions, and also marks your executable as requiring the DLL to load at startup.

In Visual Studio, you can select the project type for each project: either Static library (will produce .lib file) or Dynamic library (will produce .dll file and its corresponding .lib file).

In the Unix world, this works differently: a shared library (extension .so) is itself used during linking, and that creates the loader dependency.

Upvotes: 14

Related Questions