一、安裝 Ubuntu
Virtual Box 版本: 7.0.6 r155176 (Qt5.15.2)
Ubuntu 版本:20.04.6 (amd64, Desktop LiveDVD)
二、安裝 QT
# 官方下載
https://download.qt.io/
# 國內(nèi)鏡像下載
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 文件指導(dǎo)安裝,
需要安裝環(huán)境依賴,
# 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
報(bào)錯(cuò),這是不同系統(tǒng)編碼格式導(dǎo)致,
bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory
解決:重置文件編碼,
sudo apt-get install vim
vim configure
:set ff
:set ff=unix
報(bào)錯(cuò) cmake: not found ,需要安裝 Cmake,?
# https://cmake.org/download/
# 解壓
tar -zxvf cmake-3.27.0.tar.gz
# 進(jìn)入目錄
cd cmake-3.27.0
# 安裝
./bootstrap && make -j4 && sudo make install
?報(bào)錯(cuò) Could NOT find OpenSSL ,需要安裝?OpenSSL 依賴庫,
sudo apt-get install libssl-dev
Cmake 安裝完畢,重新配置?QT?,報(bào)錯(cuò) 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
# 進(jìn)入目錄
cd llvm-project
# 新建 build 目錄
mkdir build
# 進(jìn)入 build 目錄
cd build
# 配置
cmake -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ../llvm
# 編譯
make
# 安裝
sudo make install
安裝 LLVM 完畢,重新配置?QT,遇到 OpenGL?報(bào)錯(cuò),
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
安裝依賴后,要先清除配置緩存后重新配置,配置成功,然后執(zhí)行編譯安裝,
報(bào)錯(cuò) 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 可能會(huì)報(bào)錯(cuò),編譯時(shí)切換一下命令
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
# 添加執(zhí)行權(quán)限
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
?
四、簡單示例
運(yùn)行,報(bào)錯(cuò)找不到插件 "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 模式,在項(xiàng)目中添加環(huán)境變量,
# 配置項(xiàng)目變量
QT_DEBUG_PLUGINS=1
可以看到,搜索的是 QT 編譯源碼的目錄路徑,但實(shí)際上,QT Creator 安裝目錄下是有這個(gè)插件庫的,嘗試配置為 QT Creator 目錄下的插件路徑,
# 配置項(xiàng)目變量
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
# 默認(rèn)在系統(tǒng)路徑下
/usr/local/include/xcb
/usr/local/lib
配置環(huán)境變量,
# 配置環(huán)境變量 LD_LIBRARY_PATH
sudo vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/lib
# 使配置生效
source /etc/profile
# 或者注冊(cè)庫
sudo vim /etc/ld.so.conf
# 在底部添加
/usr/local/lib
# 重新加載
sudo /sbin/ldconfig
重新配置編譯 QT,加上參數(shù) -xcb ,不同版本的配置通過 ./configure --help 來配置參數(shù),
# 配置
./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
最終編譯安裝之后的插件產(chǎn)物有??libqxcb.so !
重新編譯運(yùn)行項(xiàng)目,正常啟動(dòng)!
五、總結(jié)
1、安裝 Perl 、Python 、C++ compiler、Openssl、Cmake、LLVM、OpenGL 軟件包與依賴
2、安裝官網(wǎng)文檔安裝 X11 依賴
3、安裝 XCB、ninja?
4、配置 QT 要有 -xcb 參數(shù)
./configure -prefix $PWD/qtbase -opensource -nomake tests -xcb
5、檢查配置文件的 QPA 的 XCB 模塊是否開啟
6、使用 Cmake 編譯安裝 QT文章來源:http://www.zghlxwxcb.cn/news/detail-768940.html
7、安裝?QT Creator ,配置組件 QT 路徑文章來源地址http://www.zghlxwxcb.cn/news/detail-768940.html
到了這里,關(guān)于Ubuntu20 源碼編譯安裝 QT6.0 教程【有詳細(xì)圖文,親測(cè)可行】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!