Encyclopedia of Linux development tools - package manager yum | vim editor | gcc/g++ compiler | automated build tool Make/Makefile | gdb debugging tool

Table of contents

Ⅰ. Linux package manager yum

First, yum background knowledge

Second, the basic use of yum

1. Check the software package

2. The composition of the software package name

3. Install the software

4. Uninstall the software

Ⅱ. vim editor

 First, know vim

1. vim concept 

2. Mode and switch

 Two, vim use

1. Vim instructions (important) 

command mode

insert mode

Bottom Row Mode

2. vim configuration

Ⅲ. gcc/g++ compiler

1. Compile and run the program

2. Use the gcc tool to complete the operation

(1) Pretreatment

(2) Compile (generate assembly)

(3) Assembly (generating machine code)

(4) Connection (generate executable file or library file)

3. Function library

 system function library

Dynamic linking and static linking

Ⅳ. Project automation build tool Make/Makefile

 1. Introduction to Make/Makefile

2. Simple use of Make/Makefile

Ⅴ.gdb debugging code

1. What is gdb

2.debug和release

3.gdb debugging operation

(1) Enter debugging

(2) Display source code

(3) Addition, deletion and information display of breakpoints

(4) run to run the debugger and continue to continue running

(6) View the value of a variable

(7) Exit gdb

4. Other commands


Ⅰ. Linux package manager yum

First, yum background knowledge

  • To install software under Linux, a common method is to download the source code of the program and compile it to obtain an executable program.
  • But this is too troublesome, so some people compile some commonly used software in advance, make it into a software package (which can be understood as an installer on windows) and put it on a server, and this compilation can be easily obtained through the package manager Good software package, install it directly.
  • Packages and package managers are like the relationship between "App" and "App Store".
  • Yum (Yellow dog Updater, Modified) is a very commonly used package manager under Linux. It is mainly used in distributions such as Fedora, RedHat, and Centos.

In layman's terms, yum is like an application store on a mobile phone. If you want to download a software package, you can use yum

It is worth noting that: 

If you are using a cloud server, the yum source is generally configured. If you are using a virtual machine, you need to open CentOS-Base.repo to check whether the link in it is a domestic link. If not, you need to Manual configuration, there are many configuration methods on the Internet, just search for the corresponding Linux distribution.

In Linux, the yum source configuration file is the CentOS-Base.repo file that exists in the /etc/yum.repos.d/ directory

Second, the basic use of yum

1. Check the software package

We can use the yum list command to list which software packages are currently available for installation (the animation showing the installable program is too big to upload...)

But there are too many software packages, so we generally use the grep command to filter out the packages we need , such as finding packages with the keyword python

yum list | grep python.x86_64

2. The composition of the software package name

Major version number. Minor version number. Source program release number - software package release number. Host platform. CPU architecture

如下:
abrt-python.x86_64                       2.1.11-60.el7.centos          @os      
  • The ".x86_64" suffix indicates the installation package for the 64-bit system, and the ".i686" suffix indicates the installation package for the 32-bit system. The installation package must match the system
  • "el7" means the host platform, that is, the version of the operating system release, where "el7" means centos7/redhat7, and "el6" means centos6 or redhat6
  • @OS in the last column means the name of "Software Source", similar to "Xiaomi App Store", "Huawei App Store"
     

3. Install the software

We can install the package with the following command (where -y means direct installation without asking)

Format: yum install (-y) software name

(1) Install the sl train

sudo yum install -y sl

After the installation is complete, enter sl 

 Claw test: Press Ctrl+z to stop

(2) Install the lrzsz tool

The lrzsz tool is used to transfer files between windows machines and remote Linux machines through XShell. After installation, we can upload files by dragging or commanding .

sudo yum install -y lrzrs

lrzrs is in our official software list, but the official software list only includes a part of the tested stable and reliable software. Some software has not been included in the official software collections of Centos, Ubuntu, Kail and other related ecological platforms. If we want to use these software, we need to install the list of unofficial software collections: epel-realse

sudo yum install -y epel-realse

After the installation is complete, enter rz or directly drag the computer file into the directory to upload the file

 So uploading files is very convenient

 Precautions

  • When installing the software, it is necessary to write content to the system directory, generally need sudo or switch to the root account to complete
  • The yum installation software can only install one after the other is installed; in the process of installing a software with yum, if you try to install another software with yum, yum will report an error
  • There is a relationship between software and software, that is, a certain degree of coupling; in order to solve the problem of interdependence between software, yum sometimes installs some other software when installing a software

4. Uninstall the software

yum remove software name

We uninstalled the previously installed sl train

Ⅱ. vim editor

 First, know vim

1. vim concept 

  • The difference between vi/vim To put it simply, they are both multi-mode editors. The difference is that vim is an upgraded version of vi . It is not only compatible with all commands of vi , but also has some new features in it. For example, syntax highlighting and visual operations can be run not only on the terminal, but also on x window , mac os , windows
  • vim is a powerful text editor on the Linux system, similar to Notepad in our windows. Due to its convenient and efficient use, it has become the most powerful editor at present, but one of its biggest disadvantages is the high barrier to use.

2. Mode and switch

The vim editor has 12 modes, but we mainly use three, mastering these three can use vim.

(1) Command line mode
Control the movement of the screen cursor, delete characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode. Once vim is opened, the default mode is the command line mode .

(2) Insert mode
Only in the Insert mode, text input can be done , press the "ESC" key to return to the command line mode. This mode is the most frequent editing mode we will use later. In the command line mode, press i on the keyboard to build, or a and o can enter the insert mode.


(3) Save or exit the file in the bottom line mode , and you can also perform operations such as file replacement, string search, and line number listing. In command mode, shift+: to enter this mode. To view all your modes: open vim, type directly in the bottom line mode: help vim-modes.

The above are the main functions of the three modes. No matter in any mode, you can enter the command line mode only by pressing ESc without thinking, and then enter the insert mode or bottom line mode from the command line mode.

At this time, I borrowed a picture from a big guy:

 Two, vim use

1. Vim instructions (important) 

The following is a summary of some of the more commonly used commands

command mode

When you open vim, the default is the command mode. If you are in other modes, you only need to press ESc to return to the command mode.

Cursor movement:

Order Function
i Enter insert mode and enter characters from the current position of the cursor
a Enter insert mode and enter characters from the next position of the cursor's current position
o Enter insert mode, insert a new line and start typing characters
ESc

Enter command mode from other modes

h,j,k,l Corresponding to the left, bottom, top and right of the cursor
G Move the cursor to the end of the text
Mr For example, 6G, the cursor jumps to line 6
gg Move the cursor to the beginning of the text
^ Move the cursor to the beginning of the current line
$ Move the cursor to the end of the current line

Delete text:

Order Function
x Lowercase x, each time you press, delete a character at the cursor position
nx For example, 6x, delete 6 characters starting from the cursor
X Uppercase X, every time you press it, delete the previous character at the cursor position
nX For example, 6X, delete the 6 characters before the cursor position
dd delete the current line where the cursor is
n.d For example, 6dd, delete the following 6 lines starting from the line where the cursor is located

Copy and cut:

Order Function
yy Copy the line where the cursor is
nyy For example, 6yy, copy 6 lines including the current line down
p Paste what yy copied
np For example, 6pp, paste the copied content 6 times
is Copy the characters from the cursor position to the end of the line
new For example, 6yw, copy the 6 characters starting from the cursor position

replace:

Order Function
r Replace the character under the cursor
R Replace the character at the cursor position until the end of the line

Undo:

Order Function
u Undo the operation performed last time, press several times to undo multiple operations
ctrl+r restore undo

Change:

Order Function
cw Change the character from the cursor position to the end of the line
cnw For example, c6w, change the 6 characters starting from the cursor position

insert mode

In command mode, press i, a, or o to enter insert mode.

Insert mode is just like our Notepad in Windows, just enter characters in between.

Bottom Row Mode

In command mode, press: that is (shift + title) to enter the bottom line mode.

Order Function
set no Explicit line numbers before each line in the text
set to nine remove line numbers from text
vs filename Split screen, open multiple files for editing at the same time, press ctrl+ww to convert the file where the cursor is located
wq Save the content and exit the vim editor
q exit vim editor
w save content
q! Force quit vim editor
w! force save content
/+ character search for characters in text

The above is the basic operation of vim. Although there are many, it is difficult to remember. As long as you use it more, you will be proficient.

2. vim configuration

We use the vim editor mainly to write code. In VS2019 under Windows, when using library header files and other content, code prompts will appear, and after entering a line of statements, press Enter to automatically indent, and also There are code highlighting and so on, which make us write code more conveniently, efficiently and beautifully.

But at this time, the vim editor on Linux does not have the above functions. Writing code on it is like writing code on a notepad.

But the vim editor is configurable, and it can be configured to be the same as vs2019, with code highlighting, prompts, indentation, etc.

Without further ado, go directly to the link: 

VimForCpp: Quickly build vim into c++ IDE (gitee.com)

 or use the command

vim ~/.vimrc

Vim detailed operation:

GitHub - wsdjeg/vim-galore-zh_cn: Vim from entry to master

Ⅲ. gcc/g++ compiler

1. Compile and run the program

一个源文件(.c)变成可执行程序(.exe)需要经历四个步骤,预处理,编译,汇编,链接

使用gcc编译器来编译写的code.c源文件,然后会在当前目录下生成一个可执行程序,再运行这个可执行程序就会显示出程序的执行结果。

  • 用法:gcc 选项 源文件 -o 目标文件名
  • 功能:将源文件编程成可执行程序
  • 常用选项:
    -E:从当前文件开始,进行完预处理停止,生成的文件我们一般加后缀.i。
    -S:从当前文件开始,进行完编译停止,生成的文件我们一般加后缀.s。
    -c:从当前文件开始,进行完汇编停止,生成的文件我们一般加后缀.o。

2.使用gcc工具完成操作

格式:gcc+[选项]+要编译的文件+[选项]+[目标文件]

(1)预处理

预处理的作用:

  • 将头文件展开,复制到源文件中
  • 去掉注释
  • 进行宏替换
  • 处理条件编译

格式:gcc+-E+文件+-o+[目标文件]

实例:

gcc -E test.c -o test.i
  • “-E”是让 gcc 在预处理结束后就停止,注意必须是大写。
  • “-o”是指目标文件,“.i”文件为已经过预处理的C原始程序。

 

(2)编译(生成汇编)

编译器的作用:将预处理生成的.i文件中的内容都转换成汇编语言。

在编译阶段,gcc 首先要检查代码的规范性、语法的正确性等,以确定代码的实际要做的工作,在检查无误后,gcc 会把代码翻译成汇编语言。

格式:gcc+-S+文件+-o+[目标文件]

实例:

gcc -S test.i -o test.s
  • “-S”表示整个过程截止到编译结束,注意必须是大写

(3)汇编(生成机器码)

汇编的作用:将编译生成的.s文件中的内容都转换成二进制机器码,因为计算机只能读懂机器码。

在汇编阶段,gcc把编译阶段生成的“.s”文件转成目标文件,此时的文件都由01组成,是计算机可以看懂的二进制文件。

格式:gcc+-c+文件+-o+[目标文件]

实例:

gcc -c test.s -o test.o
  • “-c”表示整个过程截止到编译结束

(4)连接(生成可执行文件或库文件)

在成功编译之后,就进入了链接阶段,连接起不同的.o文件最终形成可执行程序。

格式:gcc+文件+-o+[目标文件]

实例:

 gcc test.o -o test

3.函数库

 系统函数库

在我们的C程序中,并没有定义 printf 的函数实现,那我们是怎样使用 printf 函数的呢?

系统把这些系统自带的函数的定义都放到了 libc.so.6 的库文件中去了,在没有特别指定时,gcc 会到系统默认的搜索路径“/usr/lib”下进行查找,也就是相当于链接到 libc.so.6 库函数中去,这样就能找到函数 printf 的定义了

动态链接和静态链接

动态链接和静态链接是指在编译和链接程序时,将程序所需的库文件链接到程序中的不同方式。

静态链接:

是将程序所需的库文件在编译时链接到程序中,生成一个独立的可执行文件。这样生成的可执行文件比较大,但是运行时不需要依赖外部库文件,因此具有较好的可移植性和稳定性。

动态链接:

是将程序所需的库文件在运行时动态加载到内存中,程序只需要链接到库文件的接口,而不需要将整个库文件复制到程序中。这样生成的可执行文件比较小,但是运行时需要依赖外部库文件,因此具有较好的灵活性和可维护性。

总的来说,静态链接适用于需要独立运行的程序,而动态链接适用于需要共享库文件的程序。

Linux上(Windows上也如此)gcc默认生成的可执行程序,是动态链接的(dynamically Linked)。

如果我们就想让test.c静态链接形成可执行程序可通过下面的语句实现:

格式:gcc 源文件 -o 可执行程序名 -static

  • 一般系统会自动携带C语言动态库,C语言静态库可能没有安装,如果没有的话就需要自己安装,安装输入
    sudo yum install glibc-static
  • 编写一个test.c程序输入
    gcc test.c -o test_s -static
  • 用file指令发现test_s是静态链接的(statically linked)

可见,静态链接形成的可执行程序存储空间是动态链接的10多倍

Ⅳ.项目自动化构建工具Make/Makefile

 1.Make/Makefile的介绍

  • makefifile带来的好处就是——“自动化编译,一旦写好,只需要一个make命令,整个工程完全自动编 译,极大的提高了软件开发的效率。
  • make是一个命令工具,是一个解释makefifile中指令的命令工具,一般来说,大多数的IDE都有这个命 令,比如:DelphimakeVisual C++nmakeLinuxGNUmake。可见,makefifile都成为了一 种在工程方面的编译方法。
  • make是一条命令,makefifile是一个文件,两个搭配使用,完成项目自动化构建。

2.Make/Makefile的简单使用

(1)Make/Makefile的初始化

首先,我们需要创建一个Makefile文件,命名为Makefile。Makefile的基本格式如下:

target: dependencies 
    command

其中,target表示目标文件,dependencies表示依赖文件,command表示执行的命令(依赖方法)。

对于我们的例子,我们可以创建一个Makefile文件,内容如下:

main: main.c
    gcc -o main main.c

这个Makefile文件表示,我们的目标文件是main,依赖文件是main.c,执行的命令是gcc -o main main.c

接下来,我们在终端中进入程序所在的目录,执行make命令:

make

这时候,Make会自动读取Makefile文件,根据文件中的规则进行编译和构建。如果一切顺利,我们就可以在当前目录下看到一个名为main的可执行文件。

如果我们修改了main.c文件,需要重新编译程序,只需要再次执行make命令即可。

举个栗子:我们写一个测试,用main.c ,pp.h, pp.cpp三个文件实现

编写 makefile,最重要的是理解依赖关系和依赖方法:

  • 依赖关系—— 目标文件:依赖文件
  • 依赖方法—— 源文件形成目标文件需要执行的指令
  • 被.PHONY修饰的对象就是一个伪目标

(伪目标 不依赖任何文件,依赖方法是 rm -f 指令;其中 .PHONY 修饰 clean 表示其是一个伪目标,伪目标总是被执行)

建立一个Makefile文件,注意文件名必须是makefile或Makefile,不能是其他名称,否则make不能识别。

 注意:

依赖方法是 gcc 编译,依赖方法的执行指令必须以 [Tab] 键开头,特别注意不能是四个空格

例:[TAB]+g++ pp.cpp -o pp.o

(2)Make/Makefile的使用

 接下来,我们在终端中进入程序所在的目录,执行make命令:

删除make指令生成的文件则用刚刚定义的clean

make clean

 

 伪目标总是被执行 的意思就是不管你有没有文件或修改,都会执行定义的clean指令

相对应的就是make指令:只有没有文件或文件被修改才能使用

 


补充:

可通过 格式:stat+文件名  查看文件的属性信息

  • Access 指最后一次读取的时间(访问)(比如在终端上用cat、more 、less、grep、 cp 、file 一个文件时都应该更新这个时间,不过它不会每次都更新,因为访问是个非常频繁的操作,每一次我们有意无意的操作,系统都更新它的时间是没必要的)
  • Modify 指最后一次修改数据的时间(修改)(意思为更改,更改的是内容,“或者“写入)
  • Change 指最后一次修改元数据的时间(改变)(意思为改变,改变的是状态或属性,比如对一个文件或者目录作mv、chown、chgrp操作)

Ⅴ.gdb调试代码

1.什么是gdb

GDB (GNU Debugger)是一个强大的命令行工具,可以帮助开发人员调试用各种编程语言(包括 C、 C + + 和汇编)编写的程序。它适用于大多数基于 Unix 的系统,包括 Linux 和 macOS。

2.debug和release

(1)Debug称为调试版本,它在源代码的基础上不作任何优化,便于程序员调试程序。

(2)Release称为发布版本,它往往是对程序进行了各种优化,例如减小程序大小和加快运行速度等,以便用户很好地使用。

(3)这两种状态下的代码运行结果可能会不同。

只有debug版本的程序才能调试,gcc默认生成的程序是release版本的,如果想生成debug版本的可执行程序可以在编译语句后面加上-g

 若用release版本调试则

需要输入:

g++ fib.cpp -o fib -g

生成debug版本才能使用

3.gdb调试操作

以调试fib为例,简单说明最基本的几个命令,下面是gdb的操作方法。源码如下:

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int f[100];
int n;
void print(int n)
{
	for(int i=1;i<=n;i++)
		cout<<f[i]<<" ";
} 


int main()
{
	
	cout<<"hello"<<endl;
	cout<<"I"<<endl;
	cout<<"am"<<endl;
	cout<<"linux"<<endl;
	f[1]=1;
	f[2]=1;
	cin>>n;
	for(int i=3;i<=35;i++)
	{
		f[i]=f[i-1]+f[i-2];
	}
	print(n);

	cout<<n<<endl;

	return 0;
} 

(1)进入调试

格式:gdb 可执行程序文件

输入:gdb fib 进入调试,进入gdb就可以输入指令了

(2)显示源代码

格式:list/l 数字

list 默认展开源代码的前十行,如果还想看到后面的内容可以点击Enter十行十行展开,加入数字表示从第几行显示

(3)断点的添加、删除和信息显示

指令 全称 功能
b breakpoints 打一个断点
info b info break 查看当前断点
d delete breakpoints (n) 删除断点

格式:break+代码行数/b+代码行数    ||   break+代码行数+if+条件/b+代码行数+if+条件

格式:info break/info b

格式:delete+断点编号/del+断点编号

 如上图的演示,打断点的时候,b后面加行号,删除断点的时候d后加断点的序号

(4)run运行调试程序与continue继续运行

指令 全称 功能
r run 调试运行
n next 逐过程调试
s step 逐语句调试
c continue 运行到下一个断点处停止

格式:run/r        r 开始运行到断点

格式:continue/c    c 继续运行至断点或者程序结束

格式:next/n        next为gdb的逐语句调试,当遇到函数调用时,不进入函数体,相当于VS的F10

格式:step/s        step为gdb的逐过程调试,当遇到函数调用时,进入函数体,相当于VS的F11

 

 

(6)查看某个变量的值

指令 全称 功能
p print 临时查看变量值
display 常显示变量
undisplay 取消变量常显示

格式:print var(var表示任何一个变量或表达式)

在调试过程中,我们需要查看当前某个变量值的时候,可使用 print 命令显示该值,但是只能显示一次下次不会再显示。

格式:display var(var表示任何一个变量或表达式)

使用 display 命令就可以一直显示该值。

 undisplay只能删除所有显示值

(7)退出gdb

VS中退出调试可以输入Shift+F5

格式:quit/q

4.其他命令

指令 全称 功能
bt breaktrace 查看函数堆栈
finish 执行完当前函数
until+n 跳转到指定行
  • until:当你厌倦了在一个循环体内单步跟踪时,这个命令可以运行程序直到退出循环体
  • until+行号: 运行至某行,不仅仅用来跳出循环
  • finish: 运行程序,直到当前函数完成返回,并打印函数返回时的堆栈地址和返回值及参数值等信息
  • call 函数(参数):调用程序中可见的函数,并传递“参数”,如:call gdb_test(55)
  • disable 断点号n:暂停第n个断点
  • enable 断点号n:开启第n个断点
  • clear 行号n:清除第n行的断点

Guess you like

Origin blog.csdn.net/weixin_73961973/article/details/130387222