Statically link against Qt 5.8 using CMake

  • Hey,

    I'm working on an application that uses Qt for its graphical user interface. The point is, that my team is not using qmake but CMake to handle the dependencies. That worked quite well during development, but now we have some deployment issues: We think the best way to deploy our application is creating a static build, because our project depends on a quite new version of Qt (at least 5.7) and most distributions don't yet ship that version.

    What i've done so far:

    • Using Arch Linux (GCC 6.3.1)
    • Downloaded the source of Qt 5.8 from the Website using this link (note that we use the open source version of Qt)
    • Configured using ./configure -static -prefix ./build -qt-zlib -qt-libjpeg -qt-libpng -qt-xcb -qt-xkbcommon -qt-freetype -qt-pcre -qt-harfbuzz
    • make && make install
    • Now there seems to be a static installation of Qt in /path/to/qt/qtbase/build

    As our project is too big, i'll show the issue with a smaller example, that doesn't work too.


    #include <QApplication>
    #include <QPushButton>
    int main(int argc, char **argv) {
      QApplication app(argc, argv);
      QPushButton button("Hello world !");;
      return app.exec();


    cmake_minimum_required(VERSION 3.2)
    find_package(Qt5Core REQUIRED)
    find_package(Qt5Widgets REQUIRED)
    add_executable(qt-test main.cpp)
    target_link_libraries(qt-test Qt5::Widgets)
    set(CMAKE_BUILD_TYPE Debug)

    Now, if I use my system installation of Qt everything works fine (Compiled using cmake /path/to/project && make).

    But if I specify my static build of Qt using cmake -D CMAKE_PREFIX_PATH=/path/to/qt/qtbase/build /path/to/project and run make, i get a bunch of undefined reference errors by the linker: (lines >=31).

    I was able to solve that manually, adding the following parameters to the linker /path/to/qt/qtbase/build/lib/*.a -lGLU -lGL -lglut -ldl -licuuc $(pkg-config --cflags --libs glib-2.0) -licutu -licuio -licudata -licui18n -pthread, but if i start the executable afterwards, the following message is displayed:

    This application failed to start because it could not find or load the Qt platform plugin "xcb"
    in "".
    Reinstalling the application may fix this problem.
    Aborted (core dumped)

    So i've got the following questions:

    • Did I configure Qt correctly? I'm not sure about the -qt-* parameters, but i figured, that compiling the 3rdparties by myself would lead to lesser dynamically linked dependencies.
    • What do I have to add to the CMakeLists.txt, to prevent the linker error described above?
    • How should i proceed with the required platfrom plugin xcb?

    I would really apprecite your help, even if you don't have an answer to all my questions.

    Thank you,

  • I decided to discard the idea of using a static build and ship the needed dynamic libraries with the release of the project.

    Although I think that the documentation should be extended concerning CMake & static building, the wiki is just about using qmake.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.