ubuntu下编译QT

 

环境

  • Ubuntu 20.04.2 LTS

  • Qt 5.14.2

下载源码

官网https://download.qt.io/archive/qt/

完整路径https://download.qt.io/archive/qt/5.14/5.14.2/single/qt-everywhere-src-5.14.2.tar.xz

解压

# 注意解压路径不要有中文
tar -xvf qt-everywhere-src-5.14.2.tar.xz

安装依赖

sudo apt install -y build-essential gcc g++ cmake libxkbcommon-x11-dev libgl1-mesa-dev libglu1-mesa-dev libfontconfig1-dev libmysqlclient-dev libxcb-xfixes0-dev libxcb-util-dev

编译源码

编写配置脚本

# vim auto_configure.sh
#! /bin/bash

QT_INSTALL_PATH="-prefix /home/cosmos/workspace/qt5.14.2_src/_install"     # Qt安装路径(自己对应修改)
QT_COMPLIER+="-platform linux-g++-64"  # 编译器

CONFIG_PARAM+="-shared "               # 静态编译
CONFIG_PARAM+="-release "             # 编译release
CONFIG_PARAM+="-recheck-all "
CONFIG_PARAM+="-nomake examples "    # 不编译examples
CONFIG_PARAM+="-nomake tests "        # 不编译tests
CONFIG_PARAM+="-no-opengl "        # 不编译tests
CONFIG_PARAM+=" -qt-xcb -xkbcommon -fontconfig -sql-mysql -qt-zlib -qt-libpng -qt-libjpeg "

# 选择Qt版本(开源, 商业), 并自动确认许可认证
CONFIG_PARAM+="-opensource "         # 编译开源版本, -commercial商业版本
CONFIG_PARAM+="-confirm-license "      # 自动确认许可认证

echo "./configure $CONFIG_PARAM $QT_COMPLIER $QT_INSTALL_PATH"
./configure $CONFIG_PARAM $QT_COMPLIER $QT_INSTALL_PATH

编译

chmod +x auto_configure.sh
./auto_configure.sh
make -j4

安装

sudo make install

配置环境变量

#! /bin/bash

QTDIR=/home/cosmos/workspace/qt5.14.2_src/_install

PATH=$QTDIR/bin:$PATH     
MANPATH=$QTDIR/doc/man:$MANPATH     
LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH     

export QTDIR PATH MANPATH LD_LIBRARY_PATH

交叉编译

编译器

  • 名称: arm-linux-gnueabihf-gcc

  • 版本: 4.9.2

qt

  • 版本: 5.14.2

下载/解压

curl -# -O https://download.qt.io/archive/qt/5.14/5.14.2/single/qt-everywhere-src-5.14.2.tar.xz
tar -xf qt-everywhere-src-5.14.2.tar.xz

配置/编译/安装

# 新建编译器配置
cd qt-everywhere-src-5.14.2
cp -rf qtbase/mkspecs/linux-arm-gnueabi-g++ qtbase/mkspecs/linux-arm-gnueabihf-g++

修改qmake.conf配置文件,指定编译器

# vim qtbase/mkspecs/linux-arm-gnueabihf-g++/qmake.conf
#
# qmake configuration for building with arm-linux-gnueabihf-g++
#

MAKEFILE_GENERATOR      = UNIX
CONFIG                 += incremental
QMAKE_INCREMENTAL_STYLE = sublib

QT_QPA_DEFAULT_PLATFORM = linuxfb
QMAKE_CFLAGS += -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard
QMAKE_CXXFLAGS += -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

# modifications to g++.conf
QMAKE_CC                = arm-linux-gnueabihf-gcc
QMAKE_CXX               = arm-linux-gnueabihf-g++
QMAKE_LINK              = arm-linux-gnueabihf-g++
QMAKE_LINK_SHLIB        = arm-linux-gnueabihf-g++

# modifications to linux.conf
QMAKE_AR                = arm-linux-gnueabihf-ar cqs
QMAKE_OBJCOPY           = arm-linux-gnueabihf-objcopy
QMAKE_NM                = arm-linux-gnueabihf-nm -P
QMAKE_STRIP             = arm-linux-gnueabihf-strip
load(qt_config)
# vim arm_auto_configure.sh
#! /bin/bash

./configure -prefix /home/cosmos/workspace/imx6ull_alientek/nfs/rootfs/usr \
-opensource \
-confirm-license \
-release \
-strip \
-shared \
-xplatform linux-arm-gnueabihf-g++ \
-optimized-qmake \
-c++std c++11 \
--rpath=no \
-pch \
-skip qt3d \
-skip qtactiveqt \
-skip qtandroidextras \
-skip qtcanvas3d \
-skip qtconnectivity \
-skip qtdatavis3d \
-skip qtdoc \
-skip qtgamepad \
-skip qtlocation \
-skip qtmacextras \
-skip qtnetworkauth \
-skip qtpurchasing \
-skip qtremoteobjects \
-skip qtscript \
-skip qtscxml \
-skip qtsensors \
-skip qtspeech \
-skip qtsvg \
-skip qttools \
-skip qttranslations \
-skip qtwayland \
-skip qtwebengine \
-skip qtwebview \
-skip qtwinextras \
-skip qtx11extras \
-skip qtxmlpatterns \
-make libs \
-make examples \
-nomake tools -nomake tests \
-gui \
-widgets \
-dbus-runtime \
--glib=no \
--iconv=no \
--pcre=qt \
--zlib=qt \
-no-openssl \
--freetype=qt \
--harfbuzz=qt \
-no-opengl \
-linuxfb \
--xcb=no \
-tslib \
--libpng=qt \
--libjpeg=qt \
--sqlite=qt \
-plugin-sql-sqlite \
-I/home/cosmos/workspace/imx6ull_alientek/nfs/rootfs/usr/include \
-L/home/cosmos/workspace/imx6ull_alientek/nfs/rootfs/usr/lib \
-recheck-all
chmod a+x arm_auto_configure.sh
./arm_auto_configure.sh
make -j4
  • 编译安装请看上面

注意事项(报错及解决办法)

报错1

configure运行报错,提示找不到动态库

# cat config.log
> /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find /lib/libc.so.6
> /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find /usr/lib/libc_nonshared.a
> /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find /lib/ld-linux-armhf.so.3
> collect2: error: ld returned 1 exit status
> make: *** [Makefile:68: verifyspec] Error 1

解决办法:

添加 -sysroot /home/cosmos/workspace/imx6ull_alientek/nfs/rootfs \ 选项,指定sysroot位置,把编译器的相关头文件和库文件拷贝到这里

报错2

头文件提示Qt requires C++11 support,qt高版本会检查编译器对C++11支持程度,这个报错表示当前编译器版本对c++11支持不完全。

In file included from ../../include/QtCore/qbasicatomic.h:1:0,
                 from ../../include/QtCore/../../src/corelib/thread/qatomic.h:46,
                 from ../../include/QtCore/qatomic.h:1,
                 from global/qglobal.h:1302,
                 from global/qt_pch.h:56:
../../include/QtCore/../../src/corelib/thread/qbasicatomic.h:61:4: error: #error "Qt requires C++11 support"
 #  error "Qt requires C++11 support"
    ^
make[3]: Leaving directory '/home/cosmos/workspace/qt_src/qt-everywhere-src-5.14.2/qtbase/src/tools/uic'

解决办法1:升级gcc版本(由于arm-linux-gnueabihf-gccimx6ull的编译器,无法升级)

解决办法2:降低qt版本,请使用5.12.9

#  下载、解压、配置、编译、安装
cd ../
curl -# -O https://download.qt.io/archive/qt/5.12/5.12.9/single/qt-everywhere-src-5.12.9.tar.xz
tar -xf qt-everywhere-src-5.12.9.tar.xz
cd qt-everywhere-src-5.12.9
cp -rf ../qt-everywhere-src-5.14.2/qtbase/mkspecs/linux-arm-gnueabihf-g++ qtbase/mkspecs/
cp ../qt-everywhere-src-5.14.2/arm_auto_configure.sh ./
./arm_auto_configure.sh
make -j8
sudo make install