3

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:

  • a type that copies the code in it into the compiled program (removing the need for a .dll link)
  • a type that adds references to a .dll file into the compiled program

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

Community
  • 1
  • 1
octopod
  • 824
  • 2
  • 10
  • 23

1 Answers1

13

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    I know this is kind of semi-unrelated to this question, but the only way for a c++ project to use the code in a library's `.lib` is by including and using its header files, correct? I'm pretty sure it works that way, but I'd just like a confirmation. – octopod Sep 13 '15 at 11:58
  • 1
    @Octopod In practice, yes. Technically, you just need correct declarations for the entities from the library you plan to use, so you *could* write them yourself (header inclusion is just pure textual inclusion, after all). But of course, that's a very unmaintainable solution. – Angew is no longer proud of SO Sep 13 '15 at 15:08