3
votes

Sorry for my English.
I want build QT application without dependencies from any shared libraries(qt libs and glibc and ALL libraries). For this I download QT sources, build it with -static option, check all *.a libraries by ldd. This libraries have not dependencies from shared .so lib. I install new QT libraries to /opt/Q5.8 and add /opt/Q5.8/bin to $PATH.
Then I create test Hello world project.
main .cpp:

#include <iostream>


using namespace std;

int main(int argc, char *argv[])
{
   cout << "Hello world!" << endl;
   return 0;
}

main.pro:

CONFIG += debug console
SOURCES += main.cpp
QMAKE_LFLAGS += -static 

Then I run qmake | make.
qmake create Makefile successful, but make output errors:

[oleg@reffum qt_static]$ make
g++ -static -o main main.o main_plugin_import.o   -L/opt/Qt5.8/lib -L/opt/Qt5.8/plugins/platforms -lqwayland-egl -lwayland-egl -lqwayland-generic -lqwayland-xcomposite-egl -lqwayland-xcomposite-glx -lQt5WaylandClient -lXcomposite -lwayland-client -lwayland-cursor -lqxcb -L/opt/Qt5.8/plugins/xcbglintegrations -lqxcb-egl-integration -lqxcb-glx-integration -lQt5XcbQpa -lxcb-xinerama -lQt5LinuxAccessibilitySupport -lQt5AccessibilitySupport -lQt5GlxSupport -lXrender -lxcb-xkb -lxcb-sync -lxcb-xfixes -lxcb-randr -lxcb-image -lxcb-shm -lxcb-keysyms -lxcb-icccm -lxcb-shape -lxcb-glx -lXi -lSM -lICE -lxcb-render-util -lxcb-render -lxkbcommon-x11 -L/opt/Qt5.8/plugins/imageformats -lqgif -lqicns -lqico -lqjp2 -ljasper -lqjpeg -ljpeg -lqmng -lmng -lqtga -lqtiff -ltiff -lqwbmp -lqwebp -lwebp -lwebpdemux -L/opt/Qt5.8/plugins/egldeviceintegrations -lqeglfs-kms-egldevice-integration -lqeglfs-kms-integration -lQt5EglFsKmsSupport -lgbm -ldrm -lqeglfs-x11-integration -lQt5EglFSDeviceIntegration -lQt5EventDispatcherSupport -lQt5ServiceSupport -lQt5ThemeSupport -lQt5DBus -ldbus-1 -lQt5FontDatabaseSupport -lQt5FbSupport -lQt5EglSupport -lXext -lQt5PlatformCompositorSupport -lQt5InputSupport -lmtdev -linput -lxkbcommon -lQt5Gui -lpng16 -lharfbuzz -lQt5DeviceDiscoverySupport -ludev -lQt5Core -licui18n -licuuc -licudata -lm -ldl -lrt -lz -lpcre16 -lgthread-2.0 -lglib-2.0 -lxcb -lX11 -lX11-xcb -lfontconfig -lfreetype -lts -lEGL -lGL -lpthread 
/usr/bin/ld: cannot find -lwayland-egl
/usr/bin/ld: cannot find -lXcomposite
/usr/bin/ld: cannot find -lwayland-client
/usr/bin/ld: cannot find -lwayland-cursor
/usr/bin/ld: cannot find -lxcb-xinerama
/usr/bin/ld: cannot find -lXrender
/usr/bin/ld: cannot find -lxcb-xkb
/usr/bin/ld: cannot find -lxcb-sync
/usr/bin/ld: cannot find -lxcb-xfixes
/usr/bin/ld: cannot find -lxcb-randr
/usr/bin/ld: cannot find -lxcb-image
/usr/bin/ld: cannot find -lxcb-shm
/usr/bin/ld: cannot find -lxcb-keysyms
/usr/bin/ld: cannot find -lxcb-icccm
/usr/bin/ld: cannot find -lxcb-shape
/usr/bin/ld: cannot find -lxcb-glx
/usr/bin/ld: cannot find -lXi
/usr/bin/ld: cannot find -lSM
/usr/bin/ld: cannot find -lICE
/usr/bin/ld: cannot find -lxcb-render-util
/usr/bin/ld: cannot find -lxcb-render
/usr/bin/ld: cannot find -lxkbcommon-x11
/usr/bin/ld: cannot find -ljpeg
/usr/bin/ld: cannot find -lmng
/usr/bin/ld: cannot find -ltiff
/usr/bin/ld: cannot find -lwebp
/usr/bin/ld: cannot find -lwebpdemux
/usr/bin/ld: cannot find -lgbm
/usr/bin/ld: cannot find -ldrm
/usr/bin/ld: cannot find -ldbus-1
/usr/bin/ld: cannot find -lXext
/usr/bin/ld: cannot find -lmtdev
/usr/bin/ld: cannot find -linput
/usr/bin/ld: cannot find -lxkbcommon
/usr/bin/ld: cannot find -lpng16
/usr/bin/ld: cannot find -lharfbuzz
/usr/bin/ld: cannot find -ludev
/usr/bin/ld: cannot find -licui18n
/usr/bin/ld: cannot find -licuuc
/usr/bin/ld: cannot find -licudata
/opt/Qt5.8/lib/libQt5Core.a(qlibrary_unix.o): In function `QLibraryPrivate::load_sys()':
qlibrary_unix.cpp:(.text+0x103a): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: cannot find -lpcre16
/usr/bin/ld: cannot find -lgthread-2.0
/usr/bin/ld: cannot find -lglib-2.0
/usr/bin/ld: cannot find -lxcb
/usr/bin/ld: cannot find -lX11
/usr/bin/ld: cannot find -lX11-xcb
/usr/bin/ld: cannot find -lfontconfig
/usr/bin/ld: cannot find -lfreetype
/usr/bin/ld: cannot find -lts
/usr/bin/ld: cannot find -lEGL
/usr/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
make: *** [Makefile:312: main] Error 1

What am I doing wrong?

2

2 Answers

2
votes

You have to download and compile Qt from source and pass in the static flag to get it to let you build your own static binaries.

When you do this, you will have a binary that doesn't require qt libraries on the target machine, but your binary will be 13+MB for the basic QtCore and QtGui and more libraries built in... compared to the 0.5+MB that is more typical for a dynamically linked Qt executable.

http://doc.qt.io/qt-5/configure-options.html

http://www.linuxfromscratch.org/blfs/view/8.0/x/qt5.html

Use a download file like this...

http://download.qt.io/archive/qt/5.8/5.8.0/single/qt-everywhere-opensource-src-5.8.0.tar.xz

Untar it, then do an appropriate configure and make command.

./configure -prefix         $QT5PREFIX \
            -sysconfdir     /etc/xdg   \
            -confirm-license           \
            -opensource                \
            -static  #added this line  \
            -dbus-linked               \
            -openssl-linked            \
            -system-harfbuzz           \
            -system-sqlite             \
            -nomake examples           \
            -no-rpath                  \
            -skip qtwebengine          &&
make

After you do that, then you can build static Qt programs all day.

Hope that helps.

0
votes

You are missing static libraries for all of Qt's dependences. Modern Linux distributions only provide shared libs. Also, you cannot statically link a current glibc.