如何静态链接qt应用程序

时间:2019-03-26 19:44:25

标签: c++ linux qt static

我有一个带有qt核心的简单控制台应用程序, 我曾经用qt 5.11构建一个项目,一切都很好,但是当它超过5.12时,出现了以下问题:

我按如下方式构建qt:

./configure -confirm-license -prefix $BASE_DIR/$QT_DIR -release -qt-pcre -qt-zlib -optimize-size -static -no-opengl -no-openssl -opensource -nomake tests -nomake examples -no-gui -no-widgets -no-dbus -no-accessibility    

qt已成功构建,但是当我使用静态标志构建应用程序时,它开始需要动态库(尽管这不是必需的,并且在qt 5.11中一切都很好)并且因此不会发生。

export PATH=$PATH:$BASE_DIR/$QT_DIR
$BASE_DIR/$QT_DIR/bin/qmake -r QMAKE_LFLAGS+="-static -static-libgcc -static-libstdc++" $BASE_DIR/CQtDeployer.pro    

这是构建错误:

g++ -static -Wl,-O1 -Wl,--gc-sections -o build/release/cqtdeployer main.o   -L/media/D/own/Console-QtDeployer/QuasarAppLib/build/release -lQuasarApp -L/media/D/own/Console-QtDeployer/Deploy/build/release -lDeploy -L/media/D/own/Console-QtDeployer/pe/pe-parser-library/build/release -lpe-parser-library -L/media/D/own/Console-QtDeployer/staticQt/lib -lQt5Core -lm /media/D/own/Console-QtDeployer/staticQt/lib/libqtpcre2.a /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/libgthread-2.0.so /usr/lib/x86_64-linux-gnu/libglib-2.0.so -lpthread   
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libdl.so'
collect2: error: ld returned 1 exit status
Makefile:117: recipe for target 'build/release/cqtdeployer' failed
make[1]: *** [build/release/cqtdeployer] Error 1
make[1]: выход из каталога «/media/D/own/Console-QtDeployer/CQtDeployer»
Makefile:178: recipe for target 'sub-CQtDeployer-make_first-ordered' failed
make: *** [sub-CQtDeployer-make_first-ordered] Error 2

已更新

这更像是qmake的错误。 事实是,我更改了将我的makefile中的链接所需的库列表

LIBS          = $(SUBLIBS) -L/media/D/own/Console-QtDeployer/QuasarAppLib/build/release -lQuasarApp -L/media/D/own/Console-QtDeployer/Deploy/build/release -lDeploy -L/media/D/own/Console-QtDeployer/pe/pe-parser-library/build/release -lpe-parser-library -L/media/D/own/Console-QtDeployer/staticQt/lib -lQt5Core -lm /media/D/own/Console-QtDeployer/staticQt/lib/libqtpcre2.a -ldl -lglib-2.0 -lpthread

一切正常,我得到了正确的二进制文件。

编辑前为:


CC            = gcc
CXX           = g++
DEFINES       = -DWITHOUT_BASE_TESTS -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB
CFLAGS        = -pipe -O2 -Wall -W -ffunction-sections -fdata-sections -D_REENTRANT -fPIC $(DEFINES)
CXXFLAGS      = -pipe -O2 -std=gnu++1y -Wall -W -ffunction-sections -fdata-sections -D_REENTRANT -fPIC $(DEFINES)
INCPATH       = -I. -I../QuasarAppLib -I../Deploy -I../pe/pe-parser-library/include -I../staticQt/include -I../staticQt/include/QtCore -I. -I../staticQt/mkspecs/linux-g++
QMAKE         = /media/D/own/Console-QtDeployer/staticQt/bin/qmake
DEL_FILE      = rm -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p
COPY          = cp -f
COPY_FILE     = cp -f
COPY_DIR      = cp -f -R
INSTALL_FILE  = install -m 644 -p
INSTALL_PROGRAM = install -m 755 -p
INSTALL_DIR   = cp -f -R
QINSTALL      = /media/D/own/Console-QtDeployer/staticQt/bin/qmake -install qinstall
QINSTALL_PROGRAM = /media/D/own/Console-QtDeployer/staticQt/bin/qmake -install qinstall -exe
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
TAR           = tar -cf
COMPRESS      = gzip -9f
DISTNAME      = cqtdeployer1.0.0
DISTDIR = /media/D/own/Console-QtDeployer/CQtDeployer/.tmp/cqtdeployer1.0.0
LINK          = g++
LFLAGS        = -static -static-libgcc -static-libstdc++ -Wl,-O1 -Wl,--gc-sections
LIBS          = $(SUBLIBS) -L/media/D/own/Console-QtDeployer/QuasarAppLib/build/release -lQuasarApp -L/media/D/own/Console-QtDeployer/Deploy/build/release -lDeploy -L/media/D/own/Console-QtDeployer/pe/pe-parser-library/build/release -lpe-parser-library -L/media/D/own/Console-QtDeployer/staticQt/lib -lQt5Core -lm /media/D/own/Console-QtDeployer/staticQt/lib/libqtpcre2.a /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/libgthread-2.0.so /usr/lib/x86_64-linux-gnu/libglib-2.0.so -lpthread   
AR            = ar cqs
RANLIB        = 
SED           = sed
STRIP         = strip

我的专业文件:

#
# Copyright (C) 2018-2019 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#

QT -= gui

CONFIG += c++14 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG(release, debug|release): {
    DESTDIR="$$PWD/build/release"
} else {
    DESTDIR="$$PWD/build/debug"
}

include('$$PWD/../QuasarAppLib/QuasarLib.pri')
include('$$PWD/../Deploy/Deploy.pri')
include('$$PWD/../pe/pe-parser-library/pe-parser-library.pri')


TARGET = cqtdeployer

SOURCES += \
        main.cpp \


win32: RC_ICONS = $$PWD/../res/icon.ico

也许您需要以某种特殊方式配置qmake?

0 个答案:

没有答案