Linux|Ubuntu使用集成开发方式编译C++程序—笔记2

Ubuntu 镜像下载:http://mirrors.ustc.edu.cn/ubuntu-releases/
官网下载:https://cn.ubuntu.com/download/desktop
下载速度都很快,我选择的版本是Ubuntu 22.04.1 LTS
虚拟机选择的是VM ware 16
Qt下载 Qt 国内镜像站下载(较快),这里给大家推荐几个国内著名的 Qt 镜像网站,主要是各个高校的。不同镜像中的文件也不一样,这里推荐中国科学技术大学的镜像。镜像中的不同目录含义在博文:Linux 安装 Qt Creator中有介绍。
中国科学技术大学:http://mirrors.ustc.edu.cn/qtproject/
清华大学:https://mirrors.tuna.tsinghua.edu.cn/qt/
北京理工大学:http://mirror.bit.edu.cn/qtproject/
中国互联网络信息中心:https://mirrors.cnnic.cn/qt/
本人下载:qt-opensource-linux-x64-5.9.0.run
配置编译环境 Qt需要用到c++11,因此使用在线安装的方式安装所需环境

sudo apt install gcc g++ make

如果不安装make会导致后续Qt编译的时候找不到make
sudo apt-get install build-essential libgl1-mesa-dev

如果不安装会导致后续Qt编译的时候出现Igc错误,依然编译不过。这些安装后就可以顺利的进入Qt安装环节了。
吐槽Red Hat,需要重新安装yum,而且会出现各种错误,即使yum安装好了依然会出现其他错误。Ubuntu一遍过。
安装Qt 将下载的文件放到Linux安装和配置C++环境—笔记1中设置的共享文件夹中。定位安装文件(qt-opensource-linux-x64-5.9.0.run)所在路径
cd /mnt/hgfs/Vmware_Share

查看路径中所在文件
ls

显示结果
qt-opensource-linux-x64-5.9.0.run

安装,依次输入如下命令。输入qt按下table键Linux系统会自动将文件名补全。
chmod +x qt-opensource-linux-x64-5.9.0.run ./qt-opensource-linux-x64-5.9.0.run

选择如下所示(这张图是当时在Red Hat截的,Ubuntu上设置都一样就不替换了):
Linux|Ubuntu使用集成开发方式编译C++程序—笔记2
文章图片

设置环境变量。设置环境变量的目的是为了在任何目录下都可以使用Qt Library。
sudo gedit /etc/profile

然后会弹出一个编辑窗口,在末尾添加以下代码:
export QTDIR=/home/Qt5.9.0/5.9/gcc_64 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$QTDIR/lib export PATH=$PATH:$QTDIR/bin

修改完成后保存退出,然后再输入:source /etc/profile ,让环境变量生效,或者重启Linux也可以。然后在终端中输入
qmake -v

显示结果
QMake version 3.1 Using Qt version 5.9.0 in /opt/Qt5.9.0/5.9/gcc_64/lib

注意
如果修改profile导致Linux无法进入图形界面(输入账户密码回重新回到系统登录界面),采用博文:linux profile设置错误,Linux修改/etc/profile导致进不去系统中的方法进行操作。
在Linux中/etc/profile 文件,这个文件一但被错误设置就会导致系统不能登陆,对于这个问题可以在命令行模式下把最近对 /etc/profile文件所作的修改去掉即可,具体操作如下:Ctrl+Alt+F1进入命令行模式:
root身份登陆
按照下面的命令执行
cdetc回车 /bin/viprofile回车(注意:在这里不那能用直接调用Vi需要指定目录即/bin/vi)

上面两个命令可以打开 /etc/profile 文件,打开后
输入
i回车(这是vi的插入操作命令)

然后找到你最近修改过的内容,恢复原来状态或者事删除。然后,按esc键退出插入,分别按:和输入wq回车,这时profile文件已经保存更新,按Ctrl+Alt+F2切换进入图形界面就可以了。
利用Qt Creator编译第一个程序 【Linux|Ubuntu使用集成开发方式编译C++程序—笔记2】Linux|Ubuntu使用集成开发方式编译C++程序—笔记2
文章图片

Linux|Ubuntu使用集成开发方式编译C++程序—笔记2
文章图片

附录 这部分内容是在Red Hat中安装Qt编译程序时候遇到的各种问题,最终无力解决并放弃,留念。
出现错误:
06:15:34: Could not start process "make" Error while building/deploying project test (kit: Desktop Qt 5.9.0 GCC 64bit) The kit Desktop Qt 5.9.0 GCC 64bit has configuration issues which might be the root cause for this problem. When executing step "Make"

执行命令
sudo dnf install build-essential

显示结果
Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Error: There are no enabled repos.

[root@linuxprobe ~]# sudo dnf install build-essential Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Error: There are no enabled repos. [root@linuxprobe ~]# rpm -qa|grep yum|xargs rpm -e --nodeps [root@linuxprobe ~]# rpm -qa|grep yum [root@linuxprobe ~]# cd /etc/yum.repos.d/ [root@linuxprobe yum.repos.d]# wget https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/yum-4.7.0-4.el8.noarch.rpm --2022-08-29 10:33:31--https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/yum-4.7.0-4.el8.noarch.rpm Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.6.249.229, 111.6.249.225, 111.6.249.224, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.6.249.229|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 209952 (205K) [application/octet-stream] Saving to: ‘yum-4.7.0-4.el8.noarch.rpm’yum-4.7.0-4.el8.noa 100%[===================>] 205.03K361KB/sin 0.6s2022-08-29 10:33:32 (361 KB/s) - ‘yum-4.7.0-4.el8.noarch.rpm’ saved [209952/209952][root@linuxprobe yum.repos.d]# wget https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/yum-utils-4.0.21-3.el8.noarch.rpm --2022-08-29 10:33:49--https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/yum-utils-4.0.21-3.el8.noarch.rpm Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.6.249.229, 111.6.249.225, 111.6.249.224, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.6.249.229|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 74276 (73K) [application/octet-stream] Saving to: ‘yum-utils-4.0.21-3.el8.noarch.rpm’yum-utils-4.0.21-3. 100%[===================>]72.54K--.-KB/sin 0.1s2022-08-29 10:33:50 (656 KB/s) - ‘yum-utils-4.0.21-3.el8.noarch.rpm’ saved [74276/74276][root@linuxprobe yum.repos.d]# rpm -ivh yum-4.7.0-4.el8.noarch.rpm yum-utils-4.0.21-3.el8.noarch.rpm --force --nodeps warning: yum-4.7.0-4.el8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY Verifying...################################# [100%] Preparing...################################# [100%] Updating / installing... 1:yum-utils-4.0.21-3.el8################################# [ 50%] 2:yum-4.7.0-4.el8################################# [100%] [root@linuxprobe yum.repos.d]# cp /etc/yum.repos.d/redhat.repo /etc/yum.repos.d/redhat.repo.bk [root@linuxprobe yum.repos.d]# vim /etc/yum.repos.d/redhat.repo [root@linuxprobe yum.repos.d]# vim /etc/yum.repos.d/redhat.repo [root@linuxprobe yum.repos.d]# yum makecache Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. EPEL402 kB/s |13 MB00:32 Last metadata expiration check: 0:00:22 ago on Mon 29 Aug 2022 10:36:47 AM CST. Metadata cache created. [root@linuxprobe yum.repos.d]# sudo yum install build-essential Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Last metadata expiration check: 0:01:13 ago on Mon 29 Aug 2022 10:36:47 AM CST. No match for argument: build-essential Error: Unable to find a match [root@linuxprobe yum.repos.d]# sudo yum install build-essential^C [root@linuxprobe yum.repos.d]#

卸载Qt 先找到qt的安装目录,默认安装在/opt目录,目录中有一个MaintenanceTool文件,可以卸载qt。如果忘记安装哪里了,可以通过命令找到。
find -name "Qt*"

找到后,定位到安装路径:
cd /home/Qt5.9.0

执行,按照向导卸载即可。
./MaintenanceTool

如果是编译安装,直接删除你安装的文件夹。

    推荐阅读