Adding External Libraries to Qt Projects

As simple as it sounds, most of the time it’s a hassle to add the required libraries to your Qt projects. You have to add the include path, the libraries and if you are aiming to have a cross-platform project, you need to account for Windows, macOS and Linux operating systems separately. Well, there are a couple a methods to simplify this a bit which I’ll describe in this tutorial.

The first and the most recommended way is by using the “Add Library” feature of Qt Creator. Let’s first see how it’s done using the GUI:

  • Assuming that you have an open project in Qt Creator, you can start by right clicking on your project and selecting the “Add Library” as seen in the following picture:
  • Then choose “External Library” in the dialog that is opened. The descriptions are very straightforward and simple. Here is how it looks like:
  • Now you need to choose the “Library file”, “Include path”, “Platform” and “Linkage” type. For the sake of simplicity and being able to describe this with a real-life example, I’ll be assuming that you want to add the most recent version of OpenCV library (version 3.4.3) to your Qt project. I’ll also assume your platform is Windows and you want Dynamic linking, which means you’ll need to have the DLL files beside your application, or added to the PATH environment variable. Here is a screenshot that shows how it looked like on my personal development machine:

As you can see above, there are a number of additional options you can choose. For instance, “Add ‘d’ suffix …” opetion makes sure that the letter “d” is appended to my library file name when I build my application in debug mode. The reason for this is that usually libraries built for windows come in debug and release mode and they are simply distinguished with a “d” suffix. For instance, in our example, when building your application in debug mode the OpenCV library will be called “OpenCV_world343d.lib”.

  • After pressing Next, you’ll be presented with a Summary dialog and you need to press the Finish button to complete adding the library to your project. The summary will look similar to the following code snippet:
win32:CONFIG(release, debug|release): LIBS += -LD:/dev/cv/opencv-3.4.3-vc14_vc15/build/x64/vc15/lib/ -lopencv_world343
else:win32:CONFIG(debug, debug|release): LIBS += -LD:/dev/cv/opencv-3.4.3-vc14_vc15/build/x64/vc15/lib/ -lopencv_world343d

INCLUDEPATH += D:/dev/cv/opencv-3.4.3-vc14_vc15/build/include
DEPENDPATH += D:/dev/cv/opencv-3.4.3-vc14_vc15/build/include

It’s important to note that the only thing that was changed after pressing the Finish button, was your project’s PRO file. In fact you can keep this snippet and simply paste it into your projects in order to add OpenCV to your library. Obviously, you can do the same for any other library. This is the second method of adding libraries to Qt projects.

The second method that was just mentioned, can be further improved by copying and pasting all the lines from the snippet above into a text file with the PRI extension and including it in the project using the include function. Here is an example:


It is assumed that the library inclusion codes that were acquired by using the Add Library feature in Qt Creator are all copied and pasted into a file called “mylibrary.pri” in D drive and that file is included in your application by using the previous code snippet. Using this method ensures you write the codes necessary for including a library only once and also in case of updating the library and change in library path or file name, there’s only one file that needs to be updated.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.