Ubuntu20 源码编译安装 QT6.0 教程【有详细图文,亲测可行】

一、安装 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-dependencies
https://doc.qt.io/archives/qt-6.0/linux-building.html

# 下载 qt6.0
https://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 4
make
make install 

1、安装 Perl 5.38.0

wget https://www.cpan.org/src/5.0/perl-5.38.0.tar.gz
tar -zxvf perl-5.38.0.tar.gz
cd perl-5.38.0/
./Configure -des -Dprefix=$HOME/localperl
make test
make 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.gz
wget https://www.openssl.org/source/openssl-3.0.10.tar.gz
tar -zxvf openssl-3.0.10.tar.gz
cd openssl-3.0.10
./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
make -j 4
make install

4、安装 python

# 安装依赖
sudo apt install libffi-dev
sudo apt install libgdbm-dev

tar -zxvf Python-3.8.8.tgz
cd Python-3.8.8/
./configure --prefix=/usr/local/python3 --with-ssl 
make -j 4
make 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

# 安装 git
sudo apt-get install git
# 克隆源代码
#git clone --depth=1 https://github.com/llvm/llvm-project.git
git 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 zip
sudo 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-dev
sudo apt-get install libxau-dev
sudo apt-get install libx11-dev

# 下载
wget https://xorg.freedesktop.org/archive/individual/proto/xcb-proto-1.15.tar.gz
wget https://xorg.freedesktop.org/archive/individual/lib/libxcb-1.15.tar.gz

# 解压
tar -zxvf xcb-proto-1.15.tar.gz
tar -zxvf libxcb-1.15.tar.gz

# 安装
cd xcb-proto-1.15/
./configure && make && sudo make install

cd libxcb-1.15/
./configure && make && sudo make install

# 默认在系统路径下
/usr/local/include/xcb
/usr/local/lib

配置环境变量,

# 配置环境变量 LD_LIBRARY_PATH
sudo vim /etc/profile
export 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

# 使用 ninja
sudo 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 路径

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Ubuntu编译Qt源码需要确保你的系统满足一些先决条件。首先,你需要安装一些必要的软件包和依赖项,包括构建工具、开发库和其他支持库。然后,你可以下载Qt源码并按照官方文档提供的步骤进行编译。 以下是一个大致的步骤概述: 1. 下载Qt源码:你可以从Qt官方网站(https://www.qt.io/download)下载最新版本的Qt源码。 2. 安装必要的软件包和依赖项:在开始编译Qt之前,你需要确保你的系统已经安装了一些必要的软件包和依赖项。你可以参考官方文档(https://doc.qt.io/qt-5/linux.html)中列出的要求,并根据你的Ubuntu版本进行适当的安装。 3. 配置Qt编译选项:在进入Qt源码目录后,运行configure命令来配置编译选项。你可以使用不同的选项来自定义你的Qt安装。例如,如果你想启用OpenGL支持,你可以使用`-opengl`选项。具体的选项可以在官方文档中找到。 4. 编译Qt源码:在成功配置了编译选项后,运行make命令来编译Qt源码。这可能需要一些时间,具体取决于你的系统性能和源码规模。 5. 安装Qt:在编译完成后,运行make install命令来安装Qt到系统中。这将把Qt库和工具安装到指定的目录。 请注意,这只是一个大致的步骤概述。具体的步骤和命令可能因你的系统和Qt版本而有所不同。因此,建议你在编译之前仔细阅读官方文档,并遵循官方提供的详细步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余衫马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值