一、安装 Ubuntu
Virtual Box 版本: 7.0.6 r155176 (Qt5.15.2)
Ubuntu 版本:20.04.6 (amd64, Desktop LiveDVD)
二、安装 QT
# 官方下载https://download.qt.io/# 国内镜像下载https://mirrors.cloud.tencent.com/qt/https://mirrors-i.tuna.tsinghua.edu.cn# 参考官方安装文档https://doc.qt.io/archives/qt-6.0/linux-requirements.html#platform-plugin-dependencieshttps://doc.qt.io/archives/qt-6.0/linux-building.html# 下载 qt6.0https://mirrors.cloud.tencent.com/qt/archive/qt/6.0/6.0.0/single/qt-everywhere-src-6.0.0.zip
下载后解压,看一下 README 文件指导安装,
需要安装环境依赖,
# Perl 5.8 or later => https://www.perl.org/get.html# Python 2.7 or later# C++ compiler supporting the C++11 standard./configure -prefix $PWD/qtbase -opensource -nomake tests# make -j 4makemake install
1、安装 Perl 5.38.0
wget https://www.cpan.org/src/5.0/perl-5.38.0.tar.gztar -zxvf perl-5.38.0.tar.gzcd perl-5.38.0/./Configure -des -Dprefix=$HOME/localperlmake testmake install
2、安装 gcc
sudo apt install gcc-9 g++-9
或者直接安装 GNU 集合,
sudo apt install build-essential
3、安装 Openssl
# https://www.openssl.org/source/sudo apt install zlib1g-dev# https://www.openssl.org/source/openssl-3.0.10.tar.gzwget https://www.openssl.org/source/openssl-3.0.10.tar.gztar -zxvf openssl-3.0.10.tar.gzcd openssl-3.0.10./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlibmake -j 4make install
4、安装 python
# 安装依赖sudo apt install libffi-devsudo apt install libgdbm-devtar -zxvf Python-3.8.8.tgzcd Python-3.8.8/./configure --prefix=/usr/local/python3 --with-ssl make -j 4make install
5、安装 QT
报错,这是不同系统编码格式导致,
bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory
解决:重置文件编码,
sudo apt-get install vim
vim configure:set ff:set ff=unix
报错 cmake: not found ,需要安装 Cmake,
# https://cmake.org/download/# 解压tar -zxvf cmake-3.27.0.tar.gz# 进入目录cd cmake-3.27.0# 安装./bootstrap && make -j4 && sudo make install
报错 Could NOT find OpenSSL ,需要安装 OpenSSL 依赖库,
sudo apt-get install libssl-dev
Cmake 安装完毕,重新配置 QT ,报错 LLVM and Clang C++ libraries have not been found ,
# 解决 https://clang.llvm.org/get_started.html# 安装 gitsudo apt-get install git# 克隆源代码#git clone --depth=1 https://github.com/llvm/llvm-project.gitgit clone -b llvmorg-15.0.7 https://gitee.com/openeuler/llvm-project.git# 进入目录cd llvm-project# 新建 build 目录mkdir build # 进入 build 目录cd build# 配置cmake -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ../llvm# 编译make# 安装sudo make install
安装 LLVM 完毕,重新配置 QT,遇到 OpenGL 报错,
RROR: The OpenGL functionality tests failed! You might need to modify the include and library search paths by editing QMAKE_INCDIR_OPENGL[_ES2], QMAKE_LIBDIR_OPENGL[_ES2] and QMAKE_LIBS_OPENGL[_ES2] in the mkspec for your platform.
解决,安装依赖,
sudo apt install libgl1-mesa-dev sudo apt install libglu1-mesa-dev
安装依赖后,要先清除配置缓存后重新配置,配置成功,然后执行编译安装,
报错 undefined reference to `typeinfo for clang::ASTConsumer',原因是 LLVM/Clang is built with RTTI disabled by default,
解决:修改 LLVM 配置,重新编译安装 LLVM,
# 配置cmake -DLLVM_ENABLE_RTTI=On -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ../llvm# 开启 RTTI 编译make -j4 REQUIRES_RTTI=1
./configure -prefix $PWD/qtbase -opensource -nomake tests
# make -j4 可能会报错,编译时切换一下命令cmake --build . --parallel
sudo make install
三、安装 QT Creator
# 下载wget https://mirrors-i.tuna.tsinghua.edu.cn/qt/official_releases/qtcreator/11.0/11.0.2/qt-creator-opensource-linux-x86_64-11.0.2.run# 添加执行权限chmod +x qt-creator-opensource-linux-x86_64-11.0.2.run# 安装依赖库sudo apt-get install --assume-yes xfce4 xorg-dev libopencc2 libopencc2-data unzip zipsudo apt install --reinstall libxcb-xinerama0
# 指定安装目录/home/sam/qtcreator-11.0.2
四、简单示例
运行,报错找不到插件 "xcb",
qt.qpa.plugin: Could not find the Qt platform plugin "xcb" in ""This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc.
开启 Debug 模式,在项目中添加环境变量,
# 配置项目变量QT_DEBUG_PLUGINS=1
可以看到,搜索的是 QT 编译源码的目录路径,但实际上,QT Creator 安装目录下是有这个插件库的,尝试配置为 QT Creator 目录下的插件路径,
# 配置项目变量QT_QPA_PLATFORM_PLUGIN_PATH=/home/sam/qtcreator-11.0.2/lib/Qt/plugins/platforms
然而,QT Creator 目录下的插件路径并不生效,
最后解决方案:安装 XCB 库,
sudo apt install xcb
或者源码编译安装,
# 参考官方文档 https://xcb.freedesktop.org/DevelopersGuide/# 先安装软件包sudo apt-get install gcc git make automake autoconf libtool pkg-config libpthread-stubs0-dev libxml2-dev# 安装依赖库sudo apt-get install libxcb-util-devsudo apt-get install libxau-devsudo apt-get install libx11-dev# 下载wget https://xorg.freedesktop.org/archive/individual/proto/xcb-proto-1.15.tar.gzwget https://xorg.freedesktop.org/archive/individual/lib/libxcb-1.15.tar.gz# 解压tar -zxvf xcb-proto-1.15.tar.gztar -zxvf libxcb-1.15.tar.gz# 安装cd xcb-proto-1.15/./configure && make && sudo make installcd libxcb-1.15/./configure && make && sudo make install
# 默认在系统路径下/usr/local/include/xcb/usr/local/lib
配置环境变量,
# 配置环境变量 LD_LIBRARY_PATHsudo vim /etc/profileexport LD_LIBRARY_PATH=/usr/local/lib# 使配置生效source /etc/profile# 或者注册库sudo vim /etc/ld.so.conf# 在底部添加/usr/local/lib# 重新加载sudo /sbin/ldconfig
重新配置编译 QT,加上参数 -xcb ,不同版本的配置通过 ./configure --help 来配置参数,
# 配置./configure -prefix $PWD/qtbase -opensource -xcb -nomake tests
注意,配置信息一定要有 XCB yes 的提示,如果安装了 X11 依赖还是没有 yes,则使用 ninja 编译,
# X11 依赖参考 https://doc.qt.io/archives/qt-6.0/linux-requirements.html#platform-plugin-dependencies# 使用 ninjasudo apt install ninja-build# 重新配置./configure -prefix $PWD/qtbase -opensource -nomake tests -xcb# 编译安装cmake --build . --parallel 4
最终编译安装之后的插件产物有 libqxcb.so !
重新编译运行项目,正常启动!
五、总结
1、安装 Perl 、Python 、C++ compiler、Openssl、Cmake、LLVM、OpenGL 软件包与依赖
2、安装官网文档安装 X11 依赖
3、安装 XCB、ninja
4、配置 QT 要有 -xcb 参数
./configure -prefix $PWD/qtbase -opensource -nomake tests -xcb
5、检查配置文件的 QPA 的 XCB 模块是否开启
6、使用 Cmake 编译安装 QT
7、安装 QT Creator ,配置组件 QT 路径