Qt Dynamic Linking on Windows

This article is a quick rundown on how to build your Qt application using dynamic linking; where static linking deploys a single standalone executable that's self-contained. The benefit to dynamic linking is pretty much two-fold; if you ever want to update your application, the end-user will only have to download a few new files instead of re-downloading all the dependencies, and it's much easier to setup than static linking. All you need is main Qt Creator application and you're pretty much good-to-go.


Part 1: Building + Deploying
Depending on what you are building depends on how you deploy it; meaning that it's easy to mismatch the build and deployment resulting in various errors on the the final executable and generated dependencies. In this example, I build against MSVC2017 32-bit.



Now if you go to your release executable and try running it, you'll receive an error message such as missing Qt5Widgets.dll or Qt5Core.dll, like in the image below:



This is where we need to deploy by using the windeployqt tool. Open a new command prompt window and change directory to where your exe is located. After this locate your Qt install folder -> Version number -> the folder which matches your what you built against. This last bolded, italicized part is very important. For instance I built against MSVC2017 32-bit, the incorrect directory for this would be "winrt_x86_msvc2017" - the correct one is "msvc2017". Verify that the directory you have chosen has "windeployqt.exe" in the bin sub-directory.

Now go back to your command prompt and enter the following command:

<location to windeployqt.exe> --release <name of your exe>

For instance on my build, I use: "F:\Qt\5.12.0\msvc2017\bin\windeployqt.exe --release dbqt.exe". This will generate everything to need to run your application and you should not receive any errors. If you do, then verify you're using the correct windeployqt tool.


Part 2: External Dependencies (Libraries, Dlls)
Including external dependencies is quick but there are some Qt specifics to be aware of. This will show you the most straightforward approach, so be mindful that there are more intuitive ways. Find your project's folder which includes the .pro file. Edit the .pro file with any text editor. Add the following line (I added mine under CONFIG):
LIBS += -L"<location of your .lib file>" -l<name of your lib file (without .lib)>
For instance: "LIBS += -L"C:/Users/Alek/Documents/dbqt/lib" -lsqlite3"

In Qt Creator go to Build -> Run qmake. Now try running your application, also by ensuring you have the appropriate dll included where your built executable resides.