[Linux Getting Started Guide] Chapter 6 Commonly used development tools for Linux


foreword


1. Linux editor—vi/vim

1. Introduction to vi/vim

vi is a built-in text editor in the Linux system, which has the ability of programming. Vim can be regarded as an enhanced version of vi. It can distinguish whether the syntax is correct by font color, which is convenient for program design, and has development functions such as code completion, compilation, and error jumping.

vi and vim have three common modes:

Normal mode; the default mode is this mode. When using vim to open a file, it is the normal mode. In this mode, you can use the arrow keys to move the cursor, use the delete key to process the file content, and you can also copy and paste the file content.
Insert mode; a mode in which specific content can be edited. In the normal mode, press i, o, O, a A, R and other letters (usually just press i) to enter the editing mode.
Enter a colon in the command line mode to switch to the last line mode, and then press ESC to return to the command line mode. It is not possible to switch directly between the edit mode and the last line mode, but only through the command line mode;

2. Mutual switching between vi/vim modes

insert image description here

3. General mode

Order illustrate
x / X X is to delete the next character, X is to delete the previous character If you want to delete 6 characters, then "6x"
yy Copy the line where the cursor is located. If you want to copy 20 lines, then "20yy"
p/P p is to paste to the next line, P is to paste to the previous line
dd Cut (delete) the line where the cursor is. If you want to delete 16 lines, then " 16dd "
u revoke
Ctrl+l anti-revocation
.(decimal point) repeat last action
gg/G gg is to go back to the first row, G is to go back to the last row, if you want to go back to the 16th row, then "16G"
y1G/yG y1G is to copy all the data before the current row, and yG is to copy all the data after the current row
d1G/dG d1G is to delete all the data before the current line, dG is to delete all the data after the current line
v/V/Ctrl+v v means that the text between the beginning and the end of the cursor will be selected, V means that all the lines between the beginning and the end of the cursor will be selected, Ctrl+v means that the rectangular area formed between the beginning and the end of the cursor will be selected
shfit+zz save and exit
shfit+zq exit without saving

4. Edit mode

options effect
I、i i means "input from the current cursor position", and I means "input at the first non-space character in the current line". (commonly used)
A、a a means "start input from the next character where the current cursor is located", and A means "start input from the last character of the line where the cursor is located". (commonly used)
the This is the capitalization of the English letter o. o is to input a new line at the next line where the current cursor is; O is to input a new line at the previous line where the current cursor is! (commonly used)
R、r r will only replace the character where the cursor is located once; R will always replace the text where the cursor is located until ESC is pressed; (commonly used)

4. Command line mode

:Order illustrate
:w keep
:q quit
:wq save and exit
:q! exit without saving
:set no set line number
:set nine cancel line number
:/keyword Search files (find from the beginning of the file)
: ?keyword Search files (often from the end of the file)
: s/replaced character/replaced character/g global replacement text

2. Linux package manager — yum/RPM

1. Introduction to yum

Yum (full name Yellow dog Updater, Modified) is a shell front-end software manager in Fedora, RedHat and CentOS. Based on RPM package management, it can automatically download and install RPM packages from specified servers.可以自动处理依赖关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装;

2. YUM local source: system installation CD

目的:通知Linux服务器,通过本机的系统光盘获得软件包,并安装软件

The default system YUM source needs to be connected to foreign apache websites, and the network speed is relatively slow. You can modify the associated network YUM source to a domestic mirror website, such as Netease 163, aliyun, etc.
2.1 Install wget, wget is used to download files from the specified URL

[root@mysql-master1 ~]#yum install wget

2.2YUM configuration directory

YUM configuration directory: /etc/yum.repos.d/

2.3 backup remove or delete the official YUM

//移动到临时目录
[root@mysql-master1 ~]#mv /etc/yum.repos.d/*  /tmp
//或者删除
[root@mysql-master1 ~]#rm -rf /etc/yum.repos.d/*

2.4 Write a local YUM library configuration file

 # /etc/yum.repos.d/是YUM下载地址目录
[root@mysql-master1 ~]#vim /etc/yum.repos.d/dvd.repo
[root@mysql-master1 ~]#cat /etc/yum.repos.d/dvd.repo
[dvd]  #[dev]某一个库的名称,中括号[]是必须的
name=dvd  #name=dvd是库的说明,name是必须的
baseurl=file:///mnt/  # baseurl=file:///mnt/cdrom下载库的具体地址(重要)
gpgcheck=0  #gpgcheck=0是关闭校验 

2.5 Mount the installation CD

[root@mysql-master1 ~]#mkdir /mnt/cdrom
[root@mysql-master1 ~]#vim /root/.bashrc 
//自启动文件,跟随用户启动程序运行的程序
//作用:可以避免每次重启需重新挂载
[root@mysql-master1 ~]#mount /dev/cdrom    /mnt/cdrom
# mount挂载
# /dev/cdrom是光驱文件
# /mnt/cdrom是挂载点
#重启后,使用ls  /mnt/cdrom,查看到光盘信息即可

2.6 Using YUM to install and uninstall programs

[root@mysql-master1 ~]#yum install -y http         // 安装软件
[root@mysql-master1 ~]#systemctl  start  http      // 启动软件
[root@mysql-master1 ~]#systemctl  stop firewalld  // 关闭防火墙
[root@mysql-master1 ~]#systemctl disable firewalld  //禁用防火墙开机自启

2.7 Simulate client access

打开浏览器,输入网站服务的ip地址。http://127.0.0.1,验证网站程序部署成功

3. The operation of YUM

3.1 yum command
Syntax:
yum -[option] [parameter] //yum option -y: yes answer yes to all questions
parameter description:

parameter effect
install Install the rpm package
update update rpm package
check-update Check if there is an updated rpm software installation package available
remove Remove the specified rpm package
list show package information
clean Clear yum expired cache
deplet Show all dependencies of a yum package

3.2 Installation
Installation:

[root@mysql-master1 ~]#yum -y install  httpd vsftpd
// yum主命令 -y(yes)自动确认 install(安装) http(软件包1)   vsftpd(软件包2)  (软件包N)

Reinstall:
When the software is missing files, you can try to reinstall

[root@mysql-master1 ~]#yum -y reinstall  httpd   
// reinstall 重新安装

update install

[root@mysql-master1 ~]#yum -y update         // 更新系统
[root@mysql-master1 ~]#yum -y  update httpd  // 升级一个程序httpd

3.3 Query

[root@mysql-master1 ~]#yum list | grep mysql-community-libs
// 查询是否有mysql-community-libs,其中 带@是已经安装的

insert image description here

3.4 Uninstall

[root@mysql-master1 ~]#yum -y remove httpd  // remove移除,卸载软件包

4. Configure Ali yum source

Enter the official website address of Alibaba Mirror Station: https://developer.aliyun.com/mirror/
insert image description here
4.1 Backup

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

Pull down as shown in Figure
insert image description here
4.2 to download the new CentOS-Base.repo to /etc/yum.repos.d/
and copy it to the Linux terminal for download

[root@mysql-master1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2022-10-28 23:47:39--  https://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 39.175.99.219, 39.175.99.220, 39.175.99.224, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|39.175.99.219|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

100%[==============>] 2,523       --.-K/s 用时 0.002s  

2022-10-28 23:47:39 (1.10 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])

4.3 Run yum makecache to generate cache

[root@mysql-master1 ~]# yum makecache

[root@mysql-master1 ~]# cd /etc/yum.repos.d
[root@mysql-master1  yum.repos.d]# ls
CentOS-Base.repo  CentOS-Base.repo.bak  epel.repo  epel-testing.repo  vscode.repo  zabbix.repo
[root@mysql-master1  yum.repos.d]# vim CentOS-Base.repo

#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
"CentOS-Base.repo" 62L, 2523C                       

5. RPM introduction

RPM Package Manager (formerly red Hat Package, now a recursive abbreviation), proposed by Red Hat, is adopted by many Linux distributions, also known as binary (binary code) out-of-order compilation, which can be used directly, and cannot set personal settings and switch functions.

Package list ( 注意后缀): mysql-community-community-common-5.7.12-el7.x86_64.rpm

5.1RPM包的名称格式

RPM包:Apache - 3.0-11. el7. x86_64. rpm
RPM包解释: Apache软件包名、3.0-11版本号(version)、enterprise linux 7(el7)发布版本(Release5/6/7)、 x86_64系统平台(32/64)、.rpm文件后缀

5.2 RPM指令

语法:rpm -[选项] [参数]

选项 作用
-qa 查询安装的全部rpm软件包
-e 卸载rpm软件包
-e --nodeeps 卸载软件时,不检查依赖
-ivh 用进度条显示安装时的详细信息
-v 显示详细信息
-h 进度条

5.2.1 安装

[root@mysql-master1 ~]#cd /mnt/cdrom/Packages  
//查看包是否存在
[root@mysql-master1 ~]#ls wget-1.14-18.el7_6.1.x86_64.rpm
//下载wget-1.14-18.el7_6.1.x86_64.rpm包
[root@mysql-master1 ~]#wget-1.14-18.el7_6.1.x86_64.rpm
//安装
[root@mysql-master1 ~]#rpm -ivh wget-1.14-15.el7.x86_64.rpm
选项解释:-i(安装)、 -v(可视)、  -h(百分比)

5.2.2 查询

[root@mysql-master1 ~]#rpm -q wget  wget-1.14-15.el7.x86_64
//看到软件包的名字,就说明rpm  -q查询成功,已经安装软件

5.2.3 卸载
卸载软件包

[root@mysql-master1 ~]#rpm -evh  wget-1.14-15.el7.x86_64

再次查询,发现已经卸载

[root@mysql-master1 ~]#rpm  -q wget-1.14-15.el7.x86_64
//未安装软件包  wget-1.14-15.el7.x86_64

三、编译器 — gcc/g++

vim编辑器编写代码后,c程序使用gcc编译器,c++程序使用g++编译器。
基本使用方法:
$ gcc [选项] 编译文件 [选项] 目标文件
$ g++ [选项] 编译文件 [选项] 目标文件

先用vim 写好文件

[root@mysql-master1 ~]#vim  example.c

insert image description here

[root@mysql-master1 ~]#gcc example.c

insert image description here
默认生产的可执行文件为a.out,要想生成指定文件名可用-o+文件名。
#gcc xxx.c -o xxx

gcc命令选项:

选项 功能
-E 激活预处理,需手动重定向输出文件
-S 编译到汇编语言
-c 编译到目标代码
-o 生成指定文件
-g 生成调试信,GNU 调试器可利用该信息
-static 采用静态链接库文件
- shared 此选项将尽量使用动态库,所以生成文件比较小,但是需要系统由动态库
-O0,-O1,-O2,-O3 编译器的优化选项的4个级别,-O0表示没有优化,-O1为缺省值,-O3优化级别最高
-w 不生成任何警告信息,-Wall 生成所有警告信息

在学习gcc编译器之前,先回顾一下c程序编译链接的过程:

1.预处理(进行宏替换)
2.编译(生成汇编)
3.汇编(汇编转为机器码)
4.链接(生成可执行文件或库文件)

gcc编译.c文件时,可以通过不同选项实现查看各个过程的编译结果。
insert image description here

预处理过程

预处理的功能主要包括宏定义,文件包含,条件编译,删除注释等。预处理指令主要指以#开头的语句。

[root@mysql-master1 ~]#gcc -E test.c -o test.i   //完成预处理后停止

选项-E让翻译在预处理结束后停下来,-o可以指定生成的目标文件名,预处理完的文件一般后缀为.i

编译过程

编译时,gcc 会检查代码的规范性、是否有语法错误,已确定代码所做的工作,检查无误后 gcc 会生成汇编代码。

[root@mysql-master1 ~]#gcc -S test.i -o test.s    //完成编译后停止

vim test.a 查看内容发现,在这个阶段首先要检查代码的规范性、是否有语法错误等,以确定代码的实际要做的工作,在检查无误后,把代码翻译成汇编语言

汇编过程

汇编阶段将编译所形成的汇编代码生成目标文件,也就是二进制文件。

[root@mysql-master1 ~]#gcc -c test.s -o test.o     //完成汇编后停止

vim test.o查看内容发现,程序已经从汇编指令转为机器码

链接过程

[root@mysql-master1 ~]#gcc test.o -o test        //完成链接

链接过程主要是将自己写的程序与函数或第三方库关联起来,生成可执行程序。

通常写程序时,就include了语言本身所提供的链接库文件,至于链接库文件到底是什么,接下来会讲解。

当然编译 C/C++ 程序,不需要上述这么复杂,直接执行即可:

[root@mysql-master1 ~]#gcc test.c -o test
[root@mysql-master1 ~]#gcc -o test test.c 

静态链接和动态链接

函数库的概念
语言本身会提供的库,库可以看成一套头文件和一套库文件。比如通常使用的printf,scanf等等库函数,都是实现在库文件中的,可以通过引用对应的头文件,来使用这些代码
这里函数库分为静态库和动态库两种。

1.静态库是指编译链接的时候,将所有库文件的代码全部添加到可执行代码中,在运行时不需要库文件,但是静态库文件较大,其后缀为.a
2.动态库是在程序执行时由运行时链接文件加载库,节省系统的开销,动态库文件较小,其后缀为.so

gcc 默认生成的可执行文件是动态链接的,可以通过file命令查看验证。当然要想可执行文件是静态链接,可以通过-static命令。

tips:有些服务器没有安装C语言的静态库,运行gcc -static test.c -o test_static会出现can not found -lc的错误,这里只需要运行sudo yum install glibc-static,安装静态库即可。

静态链接

静态链接的缺点:

1.浪费空间,链接时会将库中的内容放入目标文件中,所以文件的体积较大。如果有多个程序文件,那么每个程序中都会加入库文件的内容,包含相同的公共代码,造成浪费。

2.更新较为麻烦,每当库函数的代码修改了,此时就需要重新进行重新打包成lib文件,再去编译链接形成可执行程序。

静态链接的优点:

在可执行程序中已经具备了所有执行程序所需要的任何东西,不需要到其他文件中查找内容,在执行的时候运行速度快

动态链接

动态链接出现就是为了解决静态链接中的两个问题:空间浪费和更新困难。 动态链接把程序按照模块拆分成各个相对独立部分,并把链接这个过程推迟到了运行时再进行,由操作系统的装载程序加载库,将它们链接在一起形成一个完整的程序。

动态链接的优点:

1.避免空间浪费,即使多个程序依赖于同一个库,但该库不会像静态链接那样在内存中存在多份副本,而是这多个程序执行时共享副本。

2.维护比较方便,更新时只需要替换原来的目标文件,程序再次运行时,新的目标文件会自动加载到内存并链接起来。库文件与可执行文件独立,更新库文件不会对程序文件造成影响,提高了可维护性和可扩展性。

动态链接的缺点:

因为把链接推迟到了程序运行时,所以每次执行程序都需要进行链接,所以性能会有一定损失,运行速度相对慢一点

四、调试器 — gdb

当使用gcc编译程序时,出现错误,又想和vs一样进行程序调试,怎么办呢?
在Linux下也有支持程序调试的调试器:gdb
程序的发布方式有两种模式:Debug 和 Release 模式。Linux 下 gcc/g++ 编译出的可执行程序,默认是 Release 模式。要使用 gdb 调试,必须在代码编译的时候加上调试信息。
用gdb调试的指令为:#gcc 文件名

$ gcc test.c -o test_debug -g # 加上调试信息
$ gdb test_debug              # 进入gdb调试
(gdb) quit                    # 退出gdb

此时文件必须带有调试信息,使用-g指令让可执行程序带有调试信息。

常用的调试命令:

命令 功能
list/l 显示源代码
r/run 运行程序
n/next 单条运行
s/step 进入函数调用
break/b 行号 在某一行设置断点
undisplay n 取消跟踪查看序号为n的变量
display 变量名 跟踪查看变量的值
delete breakpoints n 删除序号为n的断点
info b 查看断点信息
finish 执行完当前函数
continue 执行到下一断点
p 变量名 打印变量值

使用ctrl+d或quit退出

五、Linux项目管理器 — make/makefile

实现一个项目通常会有很多文件,Linux通过Makefile来管理这些文件的运行先后等复杂操作。
make是一个指令,Makefile是一个文件,两者搭配使用,实现项目自动化构建。
Makefile文件的书写格式为:

目标文件:依赖文件
gcc [选项] 依赖文件 -o 目标文件
以example.c为例,若想把预处理、编译、汇编和链接过程的文件都生成,Makefile的书写为:

insert image description here
make命令运行结果:

If you want to delete the executable file obtained by running make, you only need to add in the Makefile:
insert image description here
tips .PHONY is a modifier symbol, so that the commands behind it are always executable

Then execute make clean.
#make clean


Summarize

以上就是今天要讲的内容,本文仅仅简单介绍了Linux常用工具的使用,从而便利我们的工作与学习。

Guess you like

Origin blog.csdn.net/guanguan12319/article/details/124357540