Compile QT and QTCreator from Linux source code

QT installation in Linux system

For the source code installation process, please refer to: GCC&make&cmake of Linux's exploration of the source code compilation process

Install online

This installation method requires a qt account, and the operation process is as follows: First, download the online installation script, the official download address . Generally, in foreign countries, there may be problems with slow downloads (no feeling). You can choose a domestic mirror, such as: University of Science and Technology of China or Tsinghua University .

There are two files in the general address, .runfor Linuxsystem, .dmgfor Macsystem, .exefor Windowsystem. Here we take the ubuntu system as an example, and use the source of the University of Science and Technology of China to download and run the script.

# 获取
wget http://mirrors.ustc.edu.cn/qtproject/official_releases/online_installers/qt-unified-linux-x64-online.run

# 分配执行权限
> chmod a+x qt-unified-linux-x64-online.run 

# 使用指定源安装,点击回车后就会出现安装界面
> ./qt-unified-linux-x64-online.run --mirror http://mirrors.ustc.edu.cn/qt/

insert image description here

Then follow the prompts to operate step by step. This method must be applied in the form of a graphical interface.

Source code compilation and installation

Also download it first, go to the official website or the domestic University of Science and Technology of China or Tsinghua University to /official_releases/qt/find the source code file, and download it.

# 以的qt6华中科qt6.4.2为例
> wget http://mirrors.ustc.edu.cn/qtproject/official_releases/qt/6.4/6.4.2/single/qt-everywhere-src-6.4.2.tar.xz

For qt6.4it, it needs to rely on many versions of dependent libraries. Here ubuntu22.04are the dependent library software that needs to be installed for successful compilation. (requires 1Gmore space)

> sudo apt install bison build-essential clang flex gperf libatspi2.0-dev libbluetooth-dev libclang-dev libcups2-dev libdrm-dev libegl1-mesa-dev libfontconfig1-dev libfreetype6-dev libgstreamer1.0-dev libhunspell-dev libnss3-dev libopengl-dev libpulse-dev libssl-dev libts-dev libx11-dev libx11-xcb-dev libxcb-glx0-dev libxcb-icccm4-dev libxcb-image0-dev libxcb-keysyms1-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-shape0-dev libxcb-shm0-dev libxcb-sync-dev libxcb-util-dev libxcb-xfixes0-dev libxcb-xinerama0-dev libxcb-xkb-dev libxcb1-dev libxcomposite-dev libxcursor-dev libxdamage-dev libxext-dev libxfixes-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev libxkbfile-dev libxrandr-dev libxrender-dev libxshmfence-dev libxshmfence1 llvm ninja-build nodejs python-is-python3 python2

Enter the root directory of the source code, compile it, and specify its installation directory (note here, try to put the files of the compilation process Cachein a separate folder, otherwise if the later compilation fails, the cached files will cause the failure of the second compilation) :

> mkdir build
> cd build
> ../configure -prefix /usr/local/qt6.4 # 指定安装路径,注意需要考虑一下权限问题,否则给普通用户指定root的权限路径,会导致后期安装的时候权限不够

There is a problem here, that is, after the installation is complete, there are *.prlquite a lot of files in the user directory. I don't know how to assign it to another directory.
During the construction process, you can see that the source code file is composed of many folders, such as qtbasefolders. In the qtsource code file, qtit is composed of multiple modules (that is, each directory in the source code file). If you want Minimal installation qt, enter qtbasethe directory to find the corresponding configurefile, execute compilation and installation, and the specified module can be installed.
The minimum installation should be just qtbaseto install the module, which is not verified. And there is a problem with this. If all are installed, the source code and the compiled file size that appear during the compilation process 30Gare different in size. Therefore, the size of the storage space must be considered. When the installation is complete, if there is not enough space, try to delete it. However, the compilation process takes a long time, so you must make sure that the installation is complete.

Build the code and install:

# 构建,这部分需要挺长时间的
cmake --build . --parellel

# 安装到 -prefix目录下
cmake --install .

If you want to build documentation for Qt Assistant:
cmake --build . --target docs
cmake --build . --target install_docs
The official documentation also mentions settings PATHand LD_LIBRARY_PATHso that qt6.4the bintools inside and libthe library files inside can be retrieved. ~/.bashrcJust add this in .

<qt安装目录>/bin/designerCheck whether the installation is successful, and check whether the interface is displayed in the installation directory .
insert image description here

problem appear

The cmake version is too low (not necessarily a problem)

cmakeThe version is too low. Although the command ubuntuis used on apt-get update, the corresponding ubunturemote warehouse cmakeis not the latest version. Consider using the new version:

wget https://github.com/Kitware/CMake/releases/download/v3.21.2/cmake-3.21.2.tar.gz
tar xf cmake-3.21.2.tar.gz
cd cmake-3.21.2
./configure
make
sudo make install

Or: do not delete the old version of cmake, but create /usr/bin/cmakea soft link, use the newly installed one cmake, and change it back after use (you can also directly use the specified cmake directly using the absolute address). download link

> wget https://cmake.org/files/v3.26/cmake-3.26.0-rc5.tar.gz

> tar -zxvf cmake-3.26.0-rc5.tar.gz

> cd cmake-3.26.0-rc5

> ./configure --prefix=/usr/local/cmake3.26


> make -j8 # 使用8个线程来编译

> make install

> cd /usr/local/cmake3.26

# 为了保证使用的自己定义的cmake,需要使用软链接,先把旧版本的可执行文件复制一份,方便恢复
> sudo cp /usr/bin/cmake /usr/bin/cmake_old
> sudo ln -sf /usr/local/cmake3.26/bin/cmake  /usr/bin/cmake # 建立软链接

# 查看全局的环境是否改变
> cmake --version

Cmake error :generator: Ninja

The reason for this problem is CMakeLists.txtthat cache files CMakeCache.txt, etc. were generated after the previous compilation. The solution is to delete the corresponding cache files. The files generated during the compilation process are: CMakeCache.txt, CMakeFiles/, config.opt, config.summary, config.tests/; In addition, there are also some cache files in each individual module, which need to be deleted, otherwise an error will be reported.

buildThe best way is to create a folder and buildexecute commands in the folder during the compilation process configure(cmake).

Install QTCreator in Linux system

The official website download address is downloaded in qtcreator , just find the corresponding version file, in each version there will be source code files ( qt-creator-opensource-src-9.0.2.zip, qt-creator-opensource-src-9.0.2.tar.gz) and online installation software ( .dmg, .run, .exe). For online installation, that is, installation based on the GUI interface, just Linuxpay attention to the operation permission in x. Here is mainly source code compilation and installation.

> wget https://download.qt.io/official_releases/qtcreator/9.0/9.0.2/qt-creator-opensource-src-9.0.2.tar.gz

# 解压
> tar -zxvf qt-creator-opensource-src-9.0.2.tar.gz

> cd qt-creator-opensource-src-9.0.2/
> mkdir build
> cd build
> cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/qt-creator
> make # 编译,需要挺久时间的。
> sudo make install # 安装

# 启动找到安装目录/usr/local/qt-creator
> ./bin/qtcreator

After running successfully, the directory appears:
insert image description here

In order to be able to Terminalstart from the command line in QtCreator. You can consider /usr/bincreating a qtcreatorscript file in the directory, the content is as follows: Among them, QT_HOMEit means QTthe installation directory

#!/bin/sh
export QT_HOME=/usr/local/qt-creator/bin
$QT_HOME/qtcreator $*

After the creation is complete, you need to give him execution permission:sudo chmod a+x /usr/bin/qtcreator

Or create a soft link /usr/binbelow . Link to the corresponding executable.

# 注意这里的/usr/local/qt-creator/bin/qtcreator是我自己安装qtcreator的目录
sudo ln -sn /usr/local/qt-creator/bin/qtcreator /usr/bin/qtcreator

mistake

./qtcreator: error while loading shared libraries: libQt6Core5Compat.so.6: cannot open shared object file: No such file or directory

QTThe reason for this error is that the previously installed liblibrary has not been added to the environment variable, so it cannot be used.
The temporary solution is to temporarily add the QTlibrary libto the environment variable

# 最好是追加,并且,这个qt目录是自己安装的目录,别弄混了
> export LD_LIBRARY_PATH=/usr/local/qt6.4/lib:$LD_LIBRARY_PATH

In order to keep the above environment variables always useful, they can be added to /etc/profile, /etc/export, ~/.bashrcor ~/.bash_profile.

Of course, you can also add this directory to the configuration file of the shared library.

# 注意这里的/usr/local/qt6.4是我安装qt的路径
> echo "/usr/local/qt6.4/lib" >> /etc/ld.so.conf
> ldconfig

qtcreator cannot be found in the search interface

In order to be able to search in the GUI qtcreator, some configuration is required.

Software installed via 源码编译安装, etc. will not be found in the list. If you want to achieve the purpose of finding applications, you need to figure out how to manage applications. Observe a special directory: . According to the name of the directory, you can get a lot of inspiration. share is shared, indicating that the setting scope here is at the system level.解压二进制applicationslinux/usr/share/applications/

Included in the QTinstallation directory are files with the following content:/share/applications.desktop

[Desktop Entry]
Type=Application
Exec=qtcreator %F
Name=Qt Creator
GenericName=C++ IDE for developing Qt applications
X-KDE-StartupNotify=true
Icon=QtProject-qtcreator
StartupWMClass=qtcreator
Terminal=false
Categories=Development;IDE;Qt;
MimeType= text/x-c++src;text/x-c++hdr;text/x-xsrc;application/x-designer;application/vnd.qt.qmakeprofile;application/vnd.qt.xml.resource;

Copy desktopthe file to /usr/share/applications, and modify its content as follows:

# 用程序启动的指令,可以带上参数运行
Exec=/usr/local/qt-creator/bin/qtcreator %F

# 图标
Icon=/usr/local/qt-creator/share/icons/hicolor/24x24/apps/QtProject-qtcreator.png

Guess you like

Origin blog.csdn.net/weixin_41012765/article/details/129405356