Linux series learning (2) - introduction and use of Vim editor, file compilation process, Makefile tool, Gdb debugger

Table of contents

introduction:

Basic commands added:

cat command:

man command:

head command:

tail command: 

find command:

grep command:

​​​​​​​​The combination of the grep

tar command:

Vim editor learning:

Common commands for switching from command mode to edit mode: 

Common commands for switching from command mode to last line mode: 

Common commands in last line mode: 

Here we write some common commands of vim: 

Command mode introduction and common commands:

ndd (delete command by line):

u (undo command):

nyy (copy n consecutive lines of content command):

p (paste command): 

gg command (move the cursor to the beginning of the file): 

G command (move the cursor to the end of the file):

shift + 4 (jump the cursor to the end of the current line):

shift + 6 (jump the cursor to the beginning of the current line): 

nG command (jump to the specified number of lines):

The last line mode command introduction: 

wq command:

w command:

q command:

q! Order:

edit mode:

Commands to configure the vim editor:

set nu command:

syntax on instructions: 

Configure the Vim editor: 

Introduction to the file compilation and running process in Linux:

File permissions and categories:

Introduction to the compilation process of C programs in Linux system:

first step:

Step two:

third step:

the fourth step:

the fifth step:

The relationship between Linux header files and source files: 

 Make command and makefile writing (project file management):

The first step: writing the text file (preparation work)

Step 2: Write the makefile, manage the project, and realize automatic compilation (.o)

Step 3: Use the make command to execute the instruction set in the makefile:

Step 4: Execute the main program in the current directory:

There are two possible error reports and solutions in the terminal:

Gdb debugger installation:

Thoughts on debugging:

MacOS installs the Gdb debugger:

Steps to install Gdb debugger in CentOS: 

Summarize:

References:


introduction:

Linux series learning catalog:

Linux series learning (1) - Linux system installation (MacOS), file system, basic commands

In the last article, we basically learned about the installation of Linux systems based on Mac systems, various Linux versions, Linux file systems, and commonly used commands in Linux. In this article, we will understand and learn the VIM editor that comes with the Linux system. The VIM editor itself has powerful functions and a variety of plug-ins. Learning VIM is also very important. At the same time, because the Mac system is very similar to the Linux system, I will directly study VIM in this article on the Mac system, and all operations can also be reproduced in Linux.

Basic commands added:

cat command:

The full name of the cat command (concatenate) is mainly used to view single or multiple files. The cat command also has the function of merging multiple files and redirecting files.

Use the cat command to display the contents of the file:

 For example, I created two files a.txt and b.txt in the test directory here. The text information in the two files is shown in the figure:

I use the cat command to merge two text files, the merged file is named c.txt, enter the command:

cat a.txt b.txt > c.txt

 So what is redirection?

input the command:

cat > c.txt

 

The system prompts us to enter, as shown in the figure, I have entered two lines of data, the first line is 1234, and the second line is 5678. After the input is completed, press Ctrl + D to end the input, and then we use the cat command to view the c.txt file:

 

As shown in the figure, we use the redirection function of cat to directly edit the c.txt file without enabling the vim editor and save it automatically.

man command:

The man command is used to view the help manual of the command. For example, I want to view the rm command here. The function of the rm command is to remove files or folders, and there are functions corresponding to different suffixes.

head command:

The head command is similar to the cat command and is used to display the contents of the first n lines of the file. The specific usage is:

head -n 文件名

For example, the c.txt file just merged has two lines of content in total. If we only need to display the content of the first line of the file, enter the command:

head -1 c.txt

As shown in the picture:

 

tail command: 

The tail command is used to display the content of the last few lines of the file. It is opposite to the head command. The specific usage is:

tail -n 文件名

For example, here I need to display the content of the last line of the c.txt file, then enter the command:

find command:

The find command is used to search the specified file in the directory tree, the specific usage is:

find 路径 -name 文件名:

For example, here I need to get the path of the text file named main.c in the Desktop directory, then enter the command:

find /Desktop -name main.c

As shown in the picture:

As shown in the figure, the path to get the main.c file is /Desktop/test/main.c

Also, if we can't find the file, the path will not be displayed:

grep command:

The grep command is used to filter out the lines of the specified string in the file. The specific usage is:

grep “字符串” 文件名

For example, I want to filter out the lines containing int strings in the main.c file, first display the files in the main.c file, and enter the command:

cat main.c

Then filter the files:

grep "int" main.c

As shown in the picture:

The combination of the grep command and the pipeline "|":

The function of using the grep command in combination with the pipeline "|" is to use the output of the previous command as the input of the next command. The specific usage is:

路径 | grep 过滤名称

For example, here I need to search for all files ending with .c in the test folder on the desktop, then enter the command:

ls /test | grep .c

As shown in the picture:

tar command:

The tar command is used to pack files. Packing files is the first step in creating a compressed file. The specific usage of the tar command is:

c 创建包文件
f 指定目标为文件而不是设备
v 显示详细过程
t 显示包中的内容而不释放
x 释放包中的内容
z GNU 版本新加的,使得 tar 有压缩和解压的功能

For example, I want to package the three files main.c add.c mul.c, enter the command:

tar cvf tar.tar add.c mud.c main.c//参数名称和作用参考上方

As shown in the figure, a file package with the suffix name tar is generated.

The second step is to generate the packaged compressed file and enter the command:

gzip tar.tar

Similarly, we can also perform the packaging and compression steps in one step, and compress and decompress commands in one step:

tar zcvf file.tar.gz main.c add.c mul.c

As shown in the figure, a compressed file is generated in one step:

 The compressed file is generated, how do we decompress the compressed file?

input the command:

gzip -d tar.tar.gz

​​​​​​​

Vim editor learning:

The vim editor has several modes, as shown in the figure:

Common commands for switching from command mode to edit mode: 

1. a //进入到当前光标后开始编辑
2. A //进入到当前光标所在行的行末开始编辑
3. i //进入当前光标位置开始编辑
4. I //进入当前光标所在行的行头开始编辑
5. o //进入当前光标下一行开始编辑
6. O //进入当前光标上一行开始编辑

Common commands for switching from command mode to last line mode: 

1. : //对文本的设置或保存工作
2. / //对文本进行全文向下搜索字符串 string
3. ? //对文本进行全文向上搜索字符串 string

Common commands in last line mode: 

1. :w //保存文本
2. :q //退出编辑
3. :wq //保存并退出
4. :q! //强制退出
5. :w newfile //另存为
6. :set nu //显示行号
7. :set nonu //取消行号
8. : set hlsearch //设置高亮搜索
9. : set nohlsearch //取消高亮搜索
10. :n,ms/oldstring/newstring //替换整个文本每行的第一个oldstring
11. :n, m s/oldstring/newstirng/g //替换整个文本所有的 oldstring
12. /string //向下搜索string
13. ?string //向上搜索 string

Here we write some common commands of vim: 

1. n dd //删除光标开始向下的 n 行
2. n yy //拷贝光标开始向下的 n 行
3. p //粘贴
4. u //撤销上一次操作
5. ctrl + r // 恢复上一次撤销操作
6. r //替换一个字符
7. shift + 6 //光标移动到当前行的行头
8. shift + 4 //光标移动到当前行的行尾
9. shift + g //光标移动到整个文本的最后一行
10. gg //光标移动到整个文本的第一行
11. n shift + g //光标移动到第 n 行
12.dnshift+g //删除光标到n行的内容
13. y n shift+g //拷贝光标到n行的内容

Command mode introduction and common commands:

After entering VIM in the terminal, you will see the following interface:

The place where I drew the red line is the version of the vim editor. Now we find that when we press most of the keys on the keyboard, the interface does not respond. Only when we enter the ":" key can we enter text in the lower left corner of the interface , which is the command mode.

Next we introduce several commands in the command mode:

ndd (delete command by line):

The ndd command is used to directly delete line by line in a text file. Note that the n here represents how many lines you need to delete starting from the cursor

Enter the data of 9 lines 1 to 9, as shown in the figure:

input the command:

9dd

As shown in the figure, the data of 9 rows is directly deleted:

u (undo command):

If we want to restore the previous text content, there is also an undo command in the vim editor, enter the command:

u

As shown in the figure, the delete operation just now is undone:

nyy (copy n consecutive lines of content command):

The nyy command is used to copy the content of n consecutive lines. For example, if I want to copy the content of the first three lines (1, 2, 3), enter the command:

3yy

As shown in the picture, there is a sentence at the bottom left:

p (paste command): 

The p command is used to paste the copied content, for example, we just copied the content of the first three lines, now we paste the content of the first three lines to the end of the file, enter the command p:

As shown in the figure, the contents of the first three lines are successfully pasted to the end.

gg command (move the cursor to the beginning of the file): 

As shown in the figure, I entered the gg command and successfully moved the cursor to the beginning:

G command (move the cursor to the end of the file):

As shown in the figure, I entered the G command and successfully moved the cursor to the end of the file:

shift + 4 (jump the cursor to the end of the current line):

As shown in the picture:

shift + 6 (jump the cursor to the beginning of the current line): 

As shown in the picture:

nG command (jump to the specified number of lines):

n represents the line you want to jump to, for example, I want to jump to line 6 now, enter the command 6G:

The last line mode command introduction: 

We can enter the command mode by pressing the Esc key in the edit mode, and then enter ":" to enter the last line mode. Similarly, the last line mode also has corresponding commands. We will introduce these commands:

wq command:

The wq command is used to save and exit the edited file.

w command:

The w command is for saving only.

q command:

The q command only exits without saving.

q! Order:

q! The command is to force exit without saving.

edit mode:

If we want to enter the editing mode of VIM, press the "I" key on the keyboard, and the word "INSERT" will appear in the lower left corner of the terminal interface, as shown in the figure:

 When "INSERT" appears, we can enter content in the vim editor, which is the editing mode.

If we randomly enter some numbers 123 in vim at this time, and intend to save and exit, enter the command: wq, as shown in the figure:

The VIM editor will prompt us that we do not have a file name yet, we enter "wq:+file name"  , for example, if I enter ":wq test.c" here, the file will be saved under the user's home directory under the name test.c.

Commands to configure the vim editor:

set nu command:

The set nu command is generally used to display the line number for the code in the vim editor. When we are in the initial mode of vim, there is no line number. As shown in the figure, I wrote a Hello program based on C language in vim. The initial state There is no line number, as shown in the figure:

We press the "Esc" key and enter the ":set nu" command to make the program code display the line number, as shown in the figure:

syntax on instructions: 

The syntax on command is generally used to display syntax highlighting in vim. As shown in the figure, although we have set the line number in vim, the initial state of the code is still not as colorful as classic editors such as VScode and Clion. Instead, all programs The codes are all black:

We press the "Esc" key and enter the ": syntax on" command to make the code display syntax highlighting, as shown in the figure:

Configure the Vim editor: 

Above we talked about several basic commands of vim. When we re-enter the file after configuring it in vim, we will find that all our previous configurations will disappear. All we have to do now is do it once and for all, and let our one-time configuration work permanently.

First of all, we need to be clear that the configuration file of vim is named vimrc, and all configurations related to vim can be performed in the vimrc file. Of course, the commands for configuring the vimrc file are also consistent with when we use vim to edit other files. We Enter the command in the terminal:

vim ~/.vimrc

After entering the command, enter the vimrc file, as shown in the figure:

Now we press the "I" key to enter the "INSERT" mode, start to configure the vimrc file, and enter the above command:

syntax on
set nu

 

Press "Esc" key to exit, press ":" key to enter command mode, input "wq" to save and exit. Then we edit the newly created test.c file again at this time, as shown in the figure:

As shown in the figure, when we configured the vimrc file and entered the test.c file again, our settings did not disappear but took effect permanently.

Similarly, we can also configure other configurations we want in the vimrc file, such as automatically aligning it, accumulating 4 spaces for the tab key, etc. Now we add some commonly used commands in vimrc, here we give some The commonly used vim configuration is for reference only;

//设置语法高亮
syntax on

//设置行号
set nu
//(set nonu)为取消行号

//设置自动对其、对其、标尺、智能对其
set autoindent
set cindent
set ruler
set smartindent

//将TAB键和4个空格区分开来,与上方makefile文件中的gcc命令前置TAB键问题相吻合
autocmd FileType make set noexpandtab

//将tab键替换为4个空格,这样有利于语法对其
set tabstop=4
set shiftwidth=4
set expandtab
set smarttab

//将编程中常用的符号改为双写并将光标置中
inoremap { {}<left>
inoremap ( ()<Left>
inoremap < <><Left>
inoremap [ []<Left>
inoremap " ""<Left>
inoremap ' ''<Left>

Here I only wrote the primary configuration of vim. Of course, there are many other vim configurations on the Internet. You can search and add them yourself if you need them.

Introduction to the file compilation and running process in Linux:

File permissions and categories:

We know that no matter in Windows, MacOS, or Linux, files are a concept based on external storage. Files are usually stored in disks or U disks. In Windows systems, files generally consist of two parts—file name and file type. In Linux, files are divided into different file attributes, they are:

r (read) read: For files, it has the permission to read the contents of the file; for directories,
  it has the permission to browse directories. 

w (write): For files, it has the authority to add and modify file content; for
  directories , it has the authority to delete and move files in the directory.

x (execute) execution: For files, the user has the permission to execute the file; for directories,
  the user has the permission to enter the directory.

Files are divided into executable files and non-executable files. For example, in C language, .c (source file) or .h (header file).o (binary object file) are all text files and are non-executable files. executable file.

Program compilation and linking process:

Here we explain these several processes: 

Preprocessing stage:

The main job of preprocessing is to insert the header file into the written code, generate a file with the extension .i to replace the original file with the extension .c, but the original file is still reserved, just the actual file during execution. A file has changed.

Compilation phase: The code in the .i file is translated into a specific assembly language. The first thing the compiler has to do is to check the standardization of the code. If there is an error, this stage will fail to determine the actual work to be done by the code. After checking, the compiler will translate all the codes into assembly language, and at the same time change the extension of the file from .I to .s.

Assembly stage: The assembly process converts the assembly code in the previous step into machine code (that is, the binary code that we call 01010100, which can be read by a computer). The file generated in this step is called the object file.o file, and the .o file is binary formatted.

Linking: The linking process is to link multiple object files and required library files (.so) into the final executable file.

Introduction to the compilation process of C programs in Linux system:

For example, we write a simple helloworld program here, and we use commands to demonstrate and analyze the process of file compilation and connection.

first step:

Create an ordinary file test1026.c, and use the vim editor to edit it, and write the C code of the helloworld program:

Step two:

To precompile test1026.c, enter the command:

gcc -E test1026.c -o test1026.i

As shown in the figure, a test1026.i file is generated, and we view this file through the vim editor:

  

As shown in the figure, there are some paths and some C language.

third step:

To compile the test1026.i file, enter the command:

gcc -S test1026.i -o test1026.s

 

As shown in the figure, a file named test1026.s is generated. According to the above introduction to the file type, this file should be the assembly language after the translation of C language. We can view this file through the vim editor:

As shown in the red circle in the figure, it is the assembly code after the C language translation.

the fourth step:

To assemble the test1026.s file, enter the command:

gcc -c test1026.s -o test1026.0

As shown in the figure, a test1026.0 file appears, and we view the contents of this file through the vim editor:

This is the content of the .o file in binary format. It can be found that the content of the file at this time has roughly become machine code.

the fifth step:

To link the test1026.0 file, enter the command:

gcc test1026.0 -o test1026

As shown in the figure, a file named test1026 is generated:

This test1026 should be the final executable file according to the steps. We use the ./ command to execute test1026 in the current directory to check whether it is really executable. Enter the command:

./test1026

As shown in the figure, the program is executed successfully!

In Linux, if we have already written the program using the vim editor, it only takes one step to generate an executable program. Let’s take the above file as an example, and only need to enter the command after writing the text file test1026.c:

gcc -o test1026 test1026.c

This command can directly output the final executable file.

The relationship between Linux header files and source files: 

In our previous article:

Data structure series learning (2) - detailed explanation of sequence table (Contiguous_List)

I once talked about the relationship between the source file and the header file in the IDE. When we write a project, we usually have to declare a function to be implemented in the header file (.h), and then declare the function declared in the header file. The function function is implemented in the corresponding source file (.cpp):

For example, when we compile a file in Linux, we declare the function in the header file, and then implement the function function in the source file. Here we write a simple function of adding two numbers in the header file and put it in the Referenced in the source file:

Function declaration in header file:

References to header files in source files:

Compile and run:

input the command:

gcc -o main main.c

 

As shown in the figure, the program executes successfully.

As we said earlier, when referencing a header file, the "" symbol is generally used, because if the <> symbol is used, it will be searched in the system path by default. If we add #include" in the main.c file at this time What happens if add.h" is changed to #include<add.h>? As shown in the picture:

An error occurred in the compilation, because the system did not find the add.h header file in the default header file path, and the storage of header files in the Linux system is placed in the /usr/include file by default, so now that we know the default path of the header file Path, we only need to migrate the add.h header file under the current path to this path, and #include<add.h> will naturally not report an error:

input the command:

mv add.h /usr/include

Compile and run again:

As shown in the figure, the compilation is successful and the program runs successfully.

But what needs to be noted here is that the /usr/include path generally stores C standard library files (such as stdio.h, stdlib.h). In general, our custom header files should not be placed under this path. This demonstration is just to force the #include<add.h> statement in the main.c file to no longer report an error. So we go to the /usr/include directory and put add.h where it is.

input the command:

cd /usr/include
mv add.h /home/parallels/dir

 Make command and makefile writing (project file management):

For example, we want to write an addition function and a multiplication function through vim and call these two functions in the main.c file.

The first step: writing the text file (preparation work)

add function:

mul function:

Call the add and mul functions in the main.c program:

Step 2: Write the makefile, manage the project, and realize automatic compilation (.o)

Create a makefile and enter the command:

touch makefile

Use the vim editor to edit the makefile and enter the command:

  1 all:main
  2 
  3 main:add.o mul.o main.o
  4     gcc -o main add.o mul.o main.o
  5 
  6 add.o:add.c
  7     gcc -c add.c
  8 mul.o:mul.c
  9     gcc -c mul.c
 10 main.o:main.c
 11     gcc -c main.c
 12 clean:
 13     rm -rf *.o main

As shown in the picture: 

 Save and exit.

Step 3: Use the make command to execute the instruction set in the makefile:

Step 4: Execute the main program in the current directory:

As shown in the figure, the function call is completed and the program is executed successfully.

There are two possible error reports and solutions in the terminal:

When using the make command, the terminal may report errors in two situations, and here we discuss them separately:

The first error:

makefile:4: *** missing separator.  Stop.

The main reason for this error is that when we configured vimrc before, we set the TAB key equal to 4 spaces, as shown in the figure, when we hit the TAB key, the line of the gcc command will not be recognized:

We return to the vimrc file in the root directory and use the vim editor to edit the vimrc file. In the vimrc file, add such a command above the command that we set the TAB key = 4 spaces:

 autocmd FileType make set noexpandtab

As shown in the picture:

Next, we re-use the vim editor to write the makefile:

This time, if we use the TAB key again, the above error will not appear:

As shown in the figure, all the commands are recognized, and the problem is solved~ 

Then there is another error, that is, the system does not install the dependency package of make at all, so the system cannot recognize the make command and cause the error:

MacOS input command:

brew install make

After the terminal executes the command, as shown in the figure:

Once done, we enter the command:

make --version

It is used to check whether our installation is successful and the make version we installed, as shown in the figure:

 As shown in the figure, the installation is complete, and the version of make is 3.81

Here are two more commands for installing make dependent packages in other systems.

CentOS input command:

yum install make

Ubuntu system input command:

apt install make

This is an example of managing multiple project files through makefile files. When we write a project such as sequence table, single linked list, and circular linked list, we usually need multiple files (header files, specific implementations of functions declared in header files) source files, test files), we can use the makefile to compile multiple project files here to generate the final executable file.

Gdb debugger installation:

Thoughts on debugging:

After we use the Vim editor to complete the writing of the C program, things are always impossible to complete in one step. In addition to program syntax errors, errors may also occur during the execution of the program. At this time, the importance of debugging is highlighted. Visual Studio Classic IDEs such as Clion and Clion all have debugging functions, and in their graphical interfaces, debugging operations are generally completed with button operations. In the visual studio compiler, there are generally two modes,

So in Vim, how do we debug the program? Therefore, we introduce the Gdb debugger. The Gdb debugger is a tool that can implement debugging in Vim. The first thing we need to do is to install the Gdb debugger. Here I write two systems (MacOS, CentOS) to install Gdb debugging The method of the device:

MacOS installs the Gdb debugger:

In MacOS, first we open the terminal, install it through brew, and enter the command:

brew install gdb

As shown in the figure, the version of the gdb debugger appears in the terminal, which means that we have completed the installation.

Steps to install Gdb debugger in CentOS: 

As shown in the figure, if the gdb debugger is not installed, the system cannot find the gdb command.

In CentOS, we use the package management tool yum to install the gdb debugger:

If your CentOS version is 8, then you may encounter the first terminal error:

 The reason: On January 31, 2022, the CentOS team removed all CentOS packages from the official mirror, and CentOS8 has ended its life cycle on December 31, 2021.

Solution: Change the download source:

input the command:

sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*

After the replacement is complete, continue to enter:

yum install gdb

If the download source is normal, you can start the download, as shown in the figure:

installing:

The installation is complete: 

Summarize:

References:

What do the attributes r, w, and x of files under linux mean?

Solve the Makefile error written by vim: Makefile: missing separator(did you mean TAB instead of 8 spaces?). Stop._Jeremy_ku's Blog-CSDN Blog

"Programmer's Self-cultivation" 

"Brother Bird's LINUX Private Kitchen"

"LINUX Platform Application Development Tutorial V5"

Guess you like

Origin blog.csdn.net/weixin_45571585/article/details/127449438