【问题标题】:How can I build fully static QT application in Linux?如何在 Linux 中构建完全静态的 QT 应用程序?
【发布时间】:2017-04-16 08:02:09
【问题描述】:

对不起我的英语。
我想构建不依赖任何共享库(qt 库和 glibc 以及所有库)的 QT 应用程序。为此,我下载 QT 源代码,使用 -static 选项构建它,通过 ldd 检查所有 *.a 库。此库不依赖于共享的 .so lib。我将新的 QT 库安装到 /opt/Q5.8 并将 /opt/Q5.8/bin 添加到 $PATH。
然后我创建测试 Hello world 项目。
主.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 

然后我运行 qmake |制作。
qmake创建Makefile成功,但是make输出错误:

[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

我做错了什么?

【问题讨论】:

    标签: linux qt gcc


    【解决方案1】:

    您必须从源代码下载和编译 Qt 并传入静态标志以获取它,以便您构建自己的静态二进制文件。

    当你这样做时,你将在目标机器上拥有一个不需要 qt 库的二进制文件,但你的二进制文件对于基本的 QtCore 和 QtGui 以及更多内置的库将是 13+MB...与0.5+MB,对于动态链接的 Qt 可执行文件来说更为典型。

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

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

    使用这样的下载文件...

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

    解压它,然后执行适当的配置和制作命令。

    ./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
    

    完成之后,您就可以整天构建静态 Qt 程序了。

    希望对您有所帮助。

    【讨论】:

    • 我几乎做到了。我使用 -static 选项构建 QT。但我没有指定 -no-rpath, -skip qtwebengine 选项。来自找不到 glib-2.0 和其他非 QT 库的链接器输出错误
    • 问题不在于 Qt,而在于它链接到的库。使用 static 选项构建 Qt 只会将可执行文件链接到静态 Qt 库,但它不会生成在没有外部依赖项的情况下运行的 Qt 构建,静态 Qt 库仍将动态链接到第 3 方库。因此,强制链接器到静态链接会导致错误,因为 OP 没有第 3 方静态库。
    【解决方案2】:

    您缺少所有 Qt 依赖项的静态库。现代 Linux 发行版仅提供共享库。此外,您不能静态链接当前的 glibc。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      相关资源
      最近更新 更多