T.Worm
T.Worm

Reputation: 432

Why Qt Creator can link dll correctly without .lib and LoadLibrary()?

Usually when we link to a dll, we must use .lib as well so that the linker can tell it is dynamic linking. However in Qt Creator we can do this without it.

I followed the instruction on youtube, and I simply the process as follows:

  1. Prepared a TestMessagePack.dll, compiled by GCC;
  2. Copy the dll to debug folder, where the exe locate in.
  3. add the codes below, which also instructs where the include files locate in.
INCLUDEPATH += "C:\Users\***\Desktop\Brand New\CMakeStuff\include\qmsgpack"

LIBS += "C:\Users\***\Desktop\Brand New\CMakeStuff\bin\TestMessagePack.dll"
  1. build and run.

Without step 2, the linker will fail. This is obvious that, Qt Creator can link to a dll without .lib. But how can the linker know without .lib file?

I need to know the principle inside

Upvotes: 1

Views: 1321

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61515

You are using one of the various MinGW?? Windows ports of GCC. On Windows, the GCC toolchain's linker (ld) supports direct linking of DLLs, unlike the Microsoft linker which requires a proxy .lib import library. Documentation:

direct linking to a dll

The cygwin/mingw ports of ld support the direct linking, including data symbols, to a dll without the usage of any import libraries.

...

And MinGW: Specify the libraries for the linker to use

MinGW supports libraries named according to the ".lib" and ".dll" conventions, in addition to the normal "lib.a" convention common on *nix systems.

...

This behaviour is given by the MingGW?? linker you are using. It means that the linkage of dynamic libraries with ld can be done in the same way on Windows as on unix-like OSes, where import libraries do not exist. Qt is not relevant.

Upvotes: 2

Related Questions