自從qt5.15版本開始,不再提供免費的離線安裝包,只能通過源碼自行編譯。剛好最近需要在ubuntu22.04下配置qt開發環境,于是寫篇文章記錄配置的過程。
其實一開始是想配置qt5.15.2的,但是在編譯配置參數這一步驟中出現如下報錯
embedsky@embedsky-virtual-machine:/opt/qt-everywhere-src-5.15.2$ make clean
make: *** No rule to make target 'clean'. Stop.
embedsky@embedsky-virtual-machine:/opt/qt-everywhere-src-5.15.2$ ./configure -prefix /opt/qt5.15.2 -opensource -confirm-license -nomake tests -nomake examples -c++std c++14
+ cd qtbase
+ /opt/qt-everywhere-src-5.15.2/qtbase/configure -top-level -prefix /opt/qt5.15.2 -opensource -confirm-license -nomake tests -nomake examples -c++std c++14
Creating qmake...
.In file included from /opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/qfloat16.h:1,from /opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h:44,from /opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/qendian.h:1,from /opt/qt-everywhere-src-5.15.2/qtbase/src/corelib/codecs/qutfcodec.cpp:43:
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:7: error: ‘numeric_limits’ is not a class template300 | class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:77: error: expected template-name before ‘<’ token300 | class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>| ^
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:77: error: expected ‘{’ before ‘<’ token
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:344:18: error: ‘numeric_limits’ is not a class template344 | template<> class numeric_limits<const QT_PREPEND_NAMESPACE(qfloat16)>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:344:69: error: ‘std::numeric_limits’ is not a template344 | template<> class numeric_limits<const QT_PREPEND_NAMESPACE(qfloat16)>| ^
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:7: note: previous declaration here300 | class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:345:28: error: expected template-name before ‘<’ token345 | : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};| ^
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:346:18: error: ‘numeric_limits’ is not a class template346 | template<> class numeric_limits<volatile QT_PREPEND_NAMESPACE(qfloat16)>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:346:72: error: ‘std::numeric_limits’ is not a template346 | template<> class numeric_limits<volatile QT_PREPEND_NAMESPACE(qfloat16)>| ^
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:7: note: previous declaration here300 | class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:347:28: error: expected template-name before ‘<’ token347 | : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};| ^
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:348:18: error: ‘numeric_limits’ is not a class template348 | template<> class numeric_limits<const volatile QT_PREPEND_NAMESPACE(qfloat16)>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:348:78: error: ‘std::numeric_limits’ is not a template348 | template<> class numeric_limits<const volatile QT_PREPEND_NAMESPACE(qfloat16)>| ^
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:7: note: previous declaration here300 | class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>| ^~~~~~~~~~~~~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:349:28: error: expected template-name before ‘<’ token349 | : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};| ^
In file included from /opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/qendian.h:1,from /opt/qt-everywhere-src-5.15.2/qtbase/src/corelib/codecs/qutfcodec.cpp:43:
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h: In static member function ‘static constexpr QSpecialInteger<S> QSpecialInteger<S>::max()’:
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h:331:30: error: ‘std::numeric_limits’ is not a template331 | { return QSpecialInteger(std::numeric_limits<T>::max()); }| ^~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h:331:54: error: incomplete type ‘std::numeric_limits’ used in nested name specifier331 | { return QSpecialInteger(std::numeric_limits<T>::max()); }| ^~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h: In static member function ‘static constexpr QSpecialInteger<S> QSpecialInteger<S>::min()’:
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h:333:30: error: ‘std::numeric_limits’ is not a template333 | { return QSpecialInteger(std::numeric_limits<T>::min()); }| ^~~
/opt/qt-everywhere-src-5.15.2/qtbase/include/QtCore/../../src/corelib/global/qendian.h:333:54: error: incomplete type ‘std::numeric_limits’ used in nested name specifier333 | { return QSpecialInteger(std::numeric_limits<T>::min()); }| ^~~
gmake: *** [Makefile:383: qutfcodec.o] Error 1
從錯誤日志來看,核心問題是:
error: ‘numeric_limits’ is not a class template
一通操作還是沒能解決,于是打算編譯其他版本的qt試試,于是便選擇了qt5.15.17
補充:后面根據這篇文章:Loading...https://bugreports.qt.io/browse/QTBUG-90395貌似這是編譯qt5.15.2時都會出現的一個bug,這個bug已經在后面的補丁版本中修復。如果非要編譯qt5.15.2,具體解決可以參考這篇博客:qt5.15.2源碼編譯出錯處理_qt #include <limits>-CSDN博客
1.執行../qt-everywhere-src-5.15.2/configure -make libs -xplatform aarch64-buildroot-linux-gnu -no-opengl報錯如下: 5.15.2/qtbase/include/QtCore/../../src/corelib/global/qfloat16.h:300:7: error: ‘numeric_limits’ is not a class template ? 300 | class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>
解決:修改.qt-everywhere-src-5.15.2/qtbase/src/corelib/global/qglobal.h
增加#include <limits>
以下是編譯qt5.15.17的過程
一、下載源碼
清華源中下載源碼Index of /qt/archive/qt/5.15/5.15.17/single/ | 清華大學開源軟件鏡像站 | Tsinghua Open Source Mirror
下載完后:cd qt-everywhere-src-5.15.17
源碼目錄如下
二、安裝相關依賴包
sudo apt update
sudo apt install build-essential perl python3 git
sudo apt install '^libxcb.*' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxext-dev libxrandr-dev libxcursor-dev libfontconfig1-dev libdbus-1-dev libglib2.0-dev libssl-dev libpng-dev libjpeg-dev libglib2.0-dev
sudo apt install libglib2.0-bin
sudo apt install libxcb-xinerama0-dev libxkbcommon-x11-dev
三、編譯配置選項
Qt5采用./configure腳本配置編譯選項,我使用的命令如下:
./configure -prefix /opt/qt5.15.17 \
-opensource -confirm-license \
-nomake tests -nomake examples \
-c++std c++14 \
-opengl desktop \-no-use-gold-linker
參數解釋:
-prefix:安裝路徑,建議使用自定義目錄如/opt/qt5.15.17
-opensource:使用開源許可證
-confirm-license:自動確認許可協議
-nomake tests -nomake examples:不編譯測試和示例,加快速度
-c++std c++14:Qt 5.15默認支持c++14,明確指定
-opengl desktop:使用系統OpenGL(如果需要軟件渲染可以用-opengl software)
-no-use-gold-linker:避免使用gold鏈接器(有時防止編譯問題)
如果還有其他編譯需求,可以加更多參數
四、編譯
執行
make -j4
編譯過程可能耗時10~30分鐘,具體取決于機器性能。
如果編譯遇到make: *** No rule to make target 'clean'等錯誤,可以用make distclean或者直接刪除源碼目錄重新解壓。
五、安裝
編譯完成后執行:
sudo make install
會將Qt安裝到-prefix
指定的目錄(/opt/qt5.15.17/)。
生成文件目錄如下
六、配置qt環境變量
echo 'export PATH=/opt/qt5.15.17/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/opt/qt5.15.17/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
七、驗證
輸入
qmake -v
輸出類似以下即可
QMake version 3.1
Using Qt version 5.15.17 in /opt/qt5.15.17/lib
八、安裝Qt Creator
在安裝完后,發現沒有像之前離線版本的方式打開Qt Creator。這是因為Qt Creator 是一個獨立的 IDE,并不包含在 Qt 源碼里,所以你自己編譯 Qt 時并不會得到 Qt Creator。
所以需要另外下載Qt Creator
sudo apt update
sudo apt install qtcreator
下載完后可以看到
此時已經有完整的ide進行開發了
九、配置kit
編譯源碼的方式不會自動幫我們配置kit,需要自己手動配置
1、配置g++、gcc
2、配置qmake
3、配置kit