Search code examples
c++qtopencvqt-creatorstatic-linking

build a standalone exe file form a QT+openCV project running in QT Creator


I just recently programmed a project in QT Creator framework which both uses QT libraries such as QT_Widget and also openCV libraries such as openCV_tracking . my project includes .ui , .pro , main.cpp and some classes with .h and .cpp files.

my .pro file is :

QT       += \
        core gui \
        concurrent widgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Version7
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += \
   c++11 \
   cmdline

SOURCES += \
    main.cpp \
    mainwindow.cpp \
    videoprocessor.cpp \
    robottracker.cpp \
    robotdetector.cpp \
    obstaclesdetector.cpp \
    pathcapture.cpp

HEADERS += \
    mainwindow.h \
    videoprocessor.h \
    robottracker.h \
    robotdetector.h \
    obstaclesdetector.h \
    pathcapture.h

FORMS += \
    mainwindow.ui

# including openCV needed files
INCLUDEPATH += E:\\MyOpenCV\\install\\include
INCLUDEPATH += C:\\openCV\\opencv\\build\\include
LIBS += -LE:\\MyOpenCV\\install\\x64\\vc15\\lib \
           -lopencv_tracking430
LIBS += -LC:\\openCV\\opencv\\build\\x64\\vc15\\lib \
           -lopencv_world430

# Default rules for deployment.
 qnx: target.path = /tmp/$${TARGET}/bin
 else: unix:!android: target.path = /opt/$${TARGET}/bin
 !isEmpty(target.path): INSTALLS += target

it runs in QT creator with no problem ; but now i wanna share it with my team and for that I want to build a standalone exe file from it which statically links all libraries used in project. can someone please guide me how can I do that with QT Creator or some other options? in the internet there were some suggestions but none worked .


Solution

  • I tried this a while ago but eventually gave up, although it definitely is possible.

    The reason Qt hides how to do it so much is that they much prefer that people dynamically link to their libraries as it both allows users to update/change the Qt dll's as they please and (which I think is the real reason) it makes it more obvious that a developer is using their suite.

    I will guide you in the direction I think should be taken, but I am not too sure how successful you will be unfortunately.

    1. First you need to build the actual Qt library statically. I will assume you are using the MinGW shipped with Qt Creator as your compiler. According to the following tutorial https://wiki.qt.io/Building_a_static_Qt_for_Windows_using_MinGW , you can download the following powershell script (ensure you have powershell installed): https://sourceforge.net/p/qtlmovie/code/ci/v1.2.16/tree/build/windows-build-qt-static.ps1?format=raw. As long as you have Qt installed under C:\Qt, and a subdirectory called "Static" in this folder, you should be able to simply run this script and wait for Qt to build into that folder.

    2. Secondly, you need to link against the Qt libs in your application. Open Qt Creator, go to Tools > Options > "Build & Run". The Qt tutorial says the following:

    Go to tab "Qt Versions". In the "qmake location" table, there must be an "Auto-detected" part and a "Manual" part. In the "Auto-detected" part, there should be one line named "Qt 5.5.0 MinGW 32bit C:\Qt\Qt5.5.0\5.5\mingw492_32\bin\qmake.exe". The "Manual" part is initially empty.

    Click "Add", browse to C:\Qt\Qt5.5.0\bin and select "qmake.exe". The version name is automatically set to "Qt 5.5.0 (5.5.0)". You should set a more meaningful name such as "Qt 5.5.0 MinGW Static 32bit"

    Then go to tab "Kits". Again, there must be an "Auto-detected" part and an initially empty "Manual" part. Click "Add". Set a meaningful name such as "Desktop Qt 5.5.0 MinGW Static 32bit". In the "Qt version" field, select your static environment, named "Qt 5.5.0 MinGW Static 32bit" if you followed the above advice.

    1. Click "Add Kit" in the upper left corner and select your static kit, named "Desktop Qt 5.5.0 MinGW Static 32bit" if you followed the above advice.

    That should be it. Unfortunately I'm unable to try it out by myself, but that should be the general procedure. A lot of the tutorials seem long-winded, but generally if you follow them step-by-step they work out in the end.

    Also, do note that they didn't refer to the "static" directory in the above instructions. I believe this is incorrect, and that is what they are actually referring to, so maybe give that a try instead, although I'm not sure.

    Good luck!