简体   繁体   中英

Deploying Qt5 application on Linux using static Qt build

I have built and installed a static build of Qt5.11 on ubuntu using the commands below:

make distclean; ./configure 
-static -release -ltcg -optimize-size -no-pch -prefix 
"/opt/qt/511-static-release" -skip webengine -nomake tools 
-nomake tests -nomake examples; make -j 8; make install;

The build finishes successfully.

I then try and link my project against this build but it fails. Here are the last few lines of output:

Compilation output (Qt-Creator)

...

../src/config/config_dialog.h -o moc_config_dialog.cpp
g++ -c -pipe -std=c++11 -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC - 
DQT_DEPRECATED_WARNINGS -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB 
-DQT_CORE_LIB -I../../my-gui-qt -I. -I../src/view/ -I./model - 
I/opt/qt/511-static-release/include -I/opt/qt/511-static- 
release/include/QtWidgets -I/opt/qt/511-static-release/include/QtGui - 
I/opt/qt/511-static-release/include/QtNetwork -I/opt/qt/511-static- 
release/include/QtCore -I. -isystem /usr/include/libdrm -I. - 
I/opt/qt/511-static-release/mkspecs/linux-g++ -o moc_config_dialog.o 
moc_config_dialog.cpp

Linker output

 g++ -Wl,-Bdynamic -lGL -static -fPIC -o my-gui-qt main.o 
 mainwindow.o task_status_delegate.o my-gui-qt_plugin_import.o  
 qrc_resources.o moc_mainwindow.o moc_task_table_view.o 
 moc_task_table_model.o moc_config_dialog.o 
 -L/opt/qt/511-static-release/lib -lQt5OpenGL -L/usr/lib/x86_64-linux-
 gnu -L/opt/qt/511-static-release/plugins/platforms 
 -lqxcb -L/opt/qt/511-static--release/plugins/xcbglintegrations 
 -lqxcb-glx-integration -lQt5XcbQpa -lQt5ServiceSupport 
 -lQt5ThemeSupport -lQt5EventDispatcherSupport 
 -lQt5FontDatabaseSupport -lfontconfig -lfreetype -lQt5GlxSupport - 
 lXext -lQt5EdidSupport -lxcb-glx -lX11-xcb -lX11 -lXi -lSM -lICE - 
 lXrender -lxcb-static -lxcb -L/opt/qt/511-static- 
 release/plugins/imageformats -lqgif -lqicns -lqico -lqjpeg -lqtga - 
 lqtiff -lqwbmp -lqwebp -L/opt/qt/511-static-release/plugins/bearer - 
 lqconnmanbearer -lqgenericbearer -lqnmbearer -lQt5DBus -lQt5Widgets - 
 lQt5Gui -lpng12 -lqtharfbuzz -lQt5Network -lQt5Core -lm -lz -licui18n 
 -licuuc -licudata -lqtpcre2 -ldl -lgthread-2.0 -lglib-2.0 -lGL - 
 lpthread 

Linker warnings

../../include/QtCore/../../src/corelib/kernel/qobjectdefs_impl.h:163:46: warning: type 'struct Function' violates one definition rule [-Wodr] .moc/qcolordialog.moc:111:57: note: a different type is defined in another translation unit

dialogs/qcolordialog.cpp:1738:41: note: the first difference of corresponding definitions is field '__pfn'

.moc/qcolordialog.moc:111:56: note: a field of same name but different type is defined in another translation unit lto1: note: type mismatch in parameter 1 dialogs/qcolordialog.cpp:179:7: note: type 'struct QWellArray' defined in anonymous namespace can not match type 'struct QWellArray'

dialogs/qcolordialog.cpp:180:1: note: the incompatible type defined in anonymous namespace in another translation unit qicohandler.cpp:95:3: warning: type 'struct BMP_INFOHDR' violates one definition rule [-Wodr]

... and some more similar warnings..

The link does complete and generate the executable but it is not run-able. I can see the file is there with:

$ file my-gui-qt

$ my-gui-qt: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), 
dynamically linked, interpreter /lib/ld64.so.1, for GNU/Linux 2.6.32, 
BuildID[sha1]=0e22025dfc5f4bf4dbba598a6f692607303b68c, not stripped

But trying to run it gives the error:

me@me-Inspiron-15-7000-Gaming:~/code/my-gui-qt/build-static$ ./my-gui- 
qt 
bash: ./my-gui-qt: No such file or directory

EDIT: I was able to reproduce this problem with the same static build of Qt 5.11 and a trivial (tiny) Qt application using the same libs. The application compiles and runs fine until I add the same QMAKE flags as for the my-gui-qt application above:

QMAKE_LFLAGS += -Wl,-Bdynamic -lGL -static

Reason being that a different interpreter is linked in to the exe:

So without the QMAKE_LFLAGS above

$file TestProject*

Gives:

TestProject: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), 
dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for 
GNU/Linux 2.6.32, 
BuildID[sha1]=993c09e6977c18772569a5a6ecb452c03a884f2d, not stripped

And runs fine. However with the flags it gives:

TestProject: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), 
dynamically linked, interpreter /lib/ld64.so.1, for GNU/Linux 2.6.32, 
BuildID[sha1]=29674cc4997c91b45f9c87bc53ee858b12639e3b, not stripped

And does not run (bash: : No such file or directory). Seems like setting the -Bdynamic flag will make gcc fallback to the generic Unix gcc interpreter /lib/lib64.so.1 which is not present in Ubuntu16.04. Not sure how to fix this though? Have tried -Wl,--dynamic-linker=/lib/ld-lsb.so.2 without success..

So it turn out this problem was one that has been answered before, I just couldn't see it.

When linking a Qt application against a static version of Qt but leaving some components (libGL.so) to be dynamically linked (ie QMAKE_LFLAGS += -Wl,-Bdynamic,- -lGL -static) the ELF interpreter may resolve to its default value (since the command above overrides the specific interpreter path), so to restore it you should specify it again (as many others have said) with something like this :

QMAKE_LFLAGS += -Wl,-dynamic-linker=/lib64/ld-linux-x86-64.so.2  

Application now runs fine. Multiple definition warnings remains when using the combination of these flags (even for a trivial Qt application).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM