17

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!

Tim Meyer
  • 12,210
  • 8
  • 64
  • 97
WewillSee
  • 279
  • 3
  • 13

3 Answers3

12

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.

Community
  • 1
  • 1
Bharath Gade
  • 153
  • 1
  • 10
6

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.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • thanks @Merlin069 for your answer. that clarifies it! One thing: when I created myLib.dll, I did not see any way to generate a lib file with the DLL file. is that on option when building the DLL? Also, all function calls in my DLL seem to be accessible without the lib file too. Is that not what the .lib file is supposed to allow? thanks – WewillSee Nov 26 '13 at 12:15
  • There should be an import library by default. – ExpatEgghead Nov 26 '13 at 12:40
  • There's likely to be a setting in your IDE to create the lib file, if it's not already generating one. As far as I understand it, without the lib file, you can load the dll separately and call functions, but the lib is required if you want to build an executable with static libraries. – TheDarkKnight Nov 26 '13 at 12:55
  • I've updated the answer for how to generate and use the lib in Qt. – TheDarkKnight Nov 26 '13 at 13:21
  • 1
    thanks @Merlin069 That link was very useful. According to the article, the whole business of lib or not lib depends on the compiler used. If the libraries were compiled using MSCV2010 you will need the .lib file. But if the library was compiled using MinGW there is no .lib at all. In my case, this explains why one library has .lib and the other one does not have one; we used different compilers. – WewillSee Nov 26 '13 at 14:06
  • CONFIG+= staticlib will create a static library instead of a DLL, but it is not related to the .lib file i think – WewillSee Nov 26 '13 at 14:07
  • Check out the last comment in this thread: http://www.qtcentre.org/archive/index.php/t-5915.html – TheDarkKnight Nov 26 '13 at 14:12
  • Yes, that it is. As the comment reads "The LIBS was required (in MSVC2010) as the "difference" of mingw." – WewillSee Nov 26 '13 at 14:19
  • Of course. Gonna do it now. Thanks for your great help @Merlin069 – WewillSee Nov 26 '13 at 14:39
0

Why is that? Why do some DLL libraries need a .lib file but other DLL libraries don't?

There are two types in linking dll : Load time dynamic linking [ Implicit ] Run time dynamic linking [ Explicit ]

If you using run time dynamic linking of dll the .lib is not used But, for load time dynamic linking of dll the.lib file is used

Anonymous
  • 1
  • 3