Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some DLL files need an additional .lib file for linking?

I have a question about library linking and .lib files...

this is the context:

  • OS = Windows
  • IDE = QT

I have created a DLL: MyLib.dll.
To use that library in my QT project, I only have to include an include path, a link to the library and use the header files:

LIBS += "C:\myPath\MyLib.dll"
INCLUDEPATH += "C:\myPath"
HEADERS += \
    ../myPath/MyLib_global.h \
    ../myPath/mylib.h

I am using a third party dll in my project: third.dll
If I do the same as in the above example, it does not work:

LIBS += "C:\myPath\third.dll" 

The third party DLL comes with a .lib file "third.lib", which I apparently need to use together with the DLL.

Why is that? Why do some DLL libraries need a .lib file but other DLL libraries don't?
Could it be that the .lib is a static library accessing the DLL?

Thanks a lot!

like image 627
WewillSee Avatar asked Nov 26 '13 11:11

WewillSee


People also ask

What is .LIB and .DLL file?

LIB vs DLLLIB is a static library where functions and procedures can be placed and called as the application is being compiled. A DLL or Dynamic Link Library does the same function but is dynamic in a sense that the application can call these libraries during run-time and not during the compilation.

What is the use of .LIB file?

A LIB file contains a library of information used by a specific program. It may store a variety of information, which may include functions and constants referenced by a program or actual objects, such as text clippings, images, or other media.

Can I use a DLL without lib?

Solution 1 Technically yes: you might use a DLL without having its import library ( . lib file), via DLL explicit linking[^]. However, if you don't have the DLL header file ( *. h ), that is you don't know DLL 's function signatures, then your task is extremely hard.

What is in a .LIB file?

lib file contains all the code and data for the library. The linker then identifies the bits it needs and puts them in the final executable. For a dynamic library, the . lib file contains a list of the exported functions and data elements from the library, and information about which DLL they came from.


2 Answers

My answer may not be specific to the context but would be useful to most developers asking the same question. This was answered by Anthony Williams

What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?

You don't need a .lib file to use a dynamic library, but without one you cannot treat functions from the DLL as normal functions in your code. Instead you must manually call LoadLibrary to load the DLL (and FreeLibrary when you're done), and GetProcAddress to obtain the address of the function or data item in the DLL. You must then cast the returned address to an appropriate pointer-to-function in order to use it.

like image 68
Bharath Gade Avatar answered Oct 15 '22 00:10

Bharath Gade


The lib file is an import library file, which allows the final executable to contain an import address table (IAT) by which all DLL function calls are referenced. Basically, allowing the functions to be looked up.

You can read about it here.

To have Qt generate the lib, add this to the .pro: -

CONFIG+= staticlib

Here's some documentation on how to create libraries.

like image 34
TheDarkKnight Avatar answered Oct 15 '22 00:10

TheDarkKnight