首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Linux中构建完全静态的QT应用程序?

如何在Linux中构建完全静态的QT应用程序?
EN

Stack Overflow用户
提问于 2017-04-16 16:02:10
回答 2查看 7.1K关注 0票数 4

对不起,我的英语不好。

我希望构建不依赖于任何共享库( QT libs和glibc以及所有库)的qt应用程序。为此,我下载了QT源代码,使用-static选项构建它,用ldd检查所有*.a库。此库不依赖于shared .so库。我在/opt/Q5.8中安装了新的QT库,并在$PATH中添加了/opt/Q5.8/bin。

然后创建test Hello world项目。

主.cpp:

代码语言:javascript
复制
#include <iostream>


using namespace std;

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

main.pro:

代码语言:javascript
复制
CONFIG += debug console
SOURCES += main.cpp
QMAKE_LFLAGS += -static 

然后运行qmake | make。

qmake创建Makefile成功,但输出错误:

代码语言:javascript
复制
[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

我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2017-04-16 21:20:37

您必须从源代码下载并编译Qt,并传入static标志以使其允许您构建自己的静态二进制文件。

当您这样做时,您将拥有一个不需要目标计算机上的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和make命令。

代码语言:javascript
复制
./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程序了。

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2017-04-16 16:22:31

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43435093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档