2017-04-16 102 views
1

對不起,我的英文。
我想從任何共享庫(qt庫和glibc和所有庫)不依賴任何構建QT應用程序。爲此我下載QT源代碼,使用-static選項構建它,通過ldd檢查所有* .a庫。這個庫沒有共享.so lib的依賴關係。我將新的QT庫安裝到/opt/Q5.8,並將/opt/Q5.8/bin添加到$ PATH。
然後我創建測試Hello世界項目。
主要的.cpp:如何在Linux中構建完全靜態的QT應用程序?

#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成功,但要輸出的錯誤:

[[email protected] 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 

我在做什麼錯?

回答

0

你缺少的所有Qt的依賴關係的靜態庫。現代Linux發行版僅提供共享庫。此外,您不能靜態鏈接當前的glibc。

1

您必須下載並從源代碼編譯的Qt,並通過靜態標誌得到它,讓你建立自己的靜態二進制文件。

當你這樣做,你將有不需要在目標機器上Qt庫的二進制,但是你的二進制文件將是13 + MB的基本QtCore和QtGui多的圖書館建於...相比對於動態鏈接的Qt可執行文件來說,0.5 + MB更爲典型。

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

其解壓縮,然後做適當的配置和make命令。

./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程序。

希望有所幫助。

+0

我差不多就是這樣。我用-static選項構建QT。但我沒有指定-no-rpath,-skip qtwebengine選項。鏈接器輸出錯誤找不到glib-2.0和其他非QT庫 –

+0

問題不在於Qt,而在於它鏈接到的庫。使用靜態選項構建Qt只會將可執行文件鏈接到靜態Qt庫,但不會生成沒有外部依賴項運行的Qt構建,靜態Qt庫仍會動態鏈接到第三方庫。所以強制鏈接器靜態鏈接導致錯誤,因爲OP沒有第三方靜態庫。 – dtech