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.
main.cpp
:#include <QApplication> #include <QPushButton> int main(int argc, char **argv) { QApplication app(argc, argv); QPushButton button("Hello world !"); button.show(); return app.exec(); }
CMakeLists.txt
:cmake_minimum_required(VERSION 3.2) project(qt-test) set(CMAKE_CXX_STANDARD 14) set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) find_package(Qt5Core REQUIRED) find_package(Qt5Widgets REQUIRED) include_directories(${Qt5Widgets_INCLUDE_DIRS}) 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 runmake
, i get a bunch of undefined reference errors by the linker: http://pastebin.com/sRNKzrnH (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,
Lukas -
-
https://github.com/koutoftimer/sqrc/issues/1#issuecomment-475271441
There is one more problem I have figured out few hours ago. It is about Cmake support of Qt's static build.
You may find out more about this in the qt's developers mailing list
TL;DR: do not use Cmake to build your project with static Qt, use qmake ONLY.