[Linux] Detailed explanation of the use of environment-based development tools

[Linux] Use of environment-based development tools

0. Preface

To develop in the Linux operating system, you must learn to use the environment-based development tools under the Linux operating system.

1. Linux package manager yum

Introduction to yum

The yum tool is a software store under the Linux operating system (centos).

image-20230716195858903

  • yum is configured directly in the Linux operating system.
  • yum can automatically find the corresponding software from the remote server and download it locally.
  • As an open source system, the Linux operating system has its own community. In the community, programmers from all over the world are maintaining and improving the code of the Linux operating system. There are many companies and individuals using the Linux operating system to develop, so their development depends on the Linux operating system. They will donate money and servers to the Linux community to facilitate the work of the Linux operating system community. Therefore, Linux also has the ability to set up servers to provide Use yum to download software for Linux operating system users.

Use of yum

View software to download:

yum list | grep 软件名
  • There are many softwares that can be installed by yum. Directly using the yum list command will display a large amount of software information, so use pipes and grep commands to filter the software you are looking for.

Check the information of SL software (the picture below shows part of the information queried):

image-20230716202336383

  • The first column of information is the software name.
  • The second column of information is the version number. (el is centos)
  • The third column of information is the software source.

Download and install the software:

sudo yum -y install 软件名
  • sudo uses an ordinary user and then executes commands as a super user. Only super users can install software.
  • sudo needs to be configured before ordinary users can use it.
  • The -y option is to avoid confirming the installation during the uninstallation process.
  • After the software is installed, it can be used by all users of the system.

Install the sl software using superuser:

image-20230716204114911

Uninstall software:

sudo yum -y remove 软件名
  • sudo uses an ordinary user and then executes the command as a super user. Only the super user can uninstall the software.
  • sudo needs to be configured before ordinary users can use it.
  • The -y option is to avoid uninstallation confirmation during the uninstallation process.

Try to remove sl software:

image-20230716202907720

Since you are an ordinary user, the deletion failed and you need to uninstall as a super user.

Uninstall the software using superuser:

image-20230716203437590

Since the -y option is not used, you need to enter y to confirm uninstalling the software.

image-20230716203408073

Uninstalled successfully.

yum source

As a software store, yum must know the download address of the software. These download addresses are generally stored in configuration files, and these configuration files become yum sources.

  • The yum source is stored in the /etc/yum.repos.d/ path.
  • Links within the yum source can be modified to update or modify the yum source.
  • The yum source of a cloud server equipped with a Linux operating system is configured and generally does not need to be updated.

Go to the directory where the yum source is located to view:

image-20230716205837273

View yum source:

image-20230716210116670

image-20230716210134794

Update from yum source:

  1. Back up the old yum source (use the mv command to backup).
  2. wget obtains the new yum source configuration file (can be obtained through network search).
  3. mv is renamed to the name of the old yum source.
  4. yum clean all && yum makecache。

About the software

  • Software stability is more important than functional diversity.
  • Although the new version of the software has many functions, there will be many problems that no one has discovered. Since they are new problems, they are generally difficult to solve in a short time.
  • Although the old version of the software has fewer functions and has certain problems, due to long-term use, existing problems generally have corresponding solutions.
  • The software is divided into official software and extended software.
  • Extension software is generally a relatively new version or some relatively novel software.
  • Official software generally includes extension software that is considered reliable after long-term use.
  • If the software to be installed cannot be found, it may be in the extended software source. You can use sudo yum install -y software name -release to find the matching yum source, and then install it.

2. Linux editor-vim

vim introduction

vim is a built-in multi-mode editor under the Linux operating system.

  • vim is an editor that can only be used to edit text.
  • Vim has 5 basic modes: command mode, insert mode, bottom line mode, replacement mode, and view mode.
  • You can check whether vim exists by directly entering the vim command under Linux.

Enter vim under Linux:

image-20230716212910687

Enter shift+: and then enter q to exit vim.

vim basic mode

image-20230802152332454

command mode

The mode you are in when you open vim is the command mode, and input cannot be done in the command mode.

  • No matter what mode you want to switch to, you must switch from command mode.
  • No matter what mode you press esc (you can press it multiple times), you can return to the command mode.

Use vim to open an empty file without performing any operations. It is in command mode:

image-20230716213605188

Insert mode

Text editing can be done in insert mode.

  • Enter (i/a/o) in command mode to enter insert mode.

image-20230728183931856

Bottom row mode

The bottom line mode is used to perform operations such as saving, finding, or replacing specified content in the file.

  • Enter insert mode by typing in command mode shift+:.
  • In bottom line mode, enter q to exit the file directly without saving changes.
  • Entering w in bottom line mode will save changes without exiting the file.
  • Entering wq in bottom line mode will save changes and exit the file.
  • Adding ! in bottom line mode will force save modifications/exit the file.

image-20230728184517581

replacement pattern

In the replacement mode, the character where the cursor is located will be replaced with the character entered on the keyboard.

  • Enter insert mode by typing in command mode shift+r.

image-20230728200306238

view mode

View mode is aimed at block editing, that is, selecting a certain area and editing text continuously, quickly and efficiently.

  • Enter view mode by typing in command mode ctrl+v.

Batch annotation in view mode:

  1. Enter view mode

image-202308021458135762. Select the line of code to comment by moving the cursor:

image-20230802145924166

  1. Enter I (capital) and //:

image-20230802150059995

  1. Press esc to complete the batch annotation:

image-20230802150131882

Uncomment batches in view mode:

  1. Enter view mode

image-20230802150258530

  1. Move the cursor to select the annotation to be deleted:

image-20230802150340214

  1. Press d to uncomment:

image-20230802150408566

Summary of commands in bottom row mode

  • Recall and remove line numbers: set nu / set nonu
  • Search for specified content: /要搜索的指定内容 – View the next match, press n, jump to the previous match, press shift+n
  • Execute Linux commands without exiting vim: !执行的命令
  • Replace the specified field with the specified field: %s/替换前的字段/替换后的字段/g
  • Open other files at the same time: vs 要打开的文件 – Hold down ctrl and press w twice quickly to switch the cursor to other files

Summary of commands in command mode

  • Copy several lines of code where the cursor is: (n)yy – If you do not enter a number before using the yy command to copy the code, it means copying the current line.

  • Paste the previously copied lines of code in the line next to the line where the cursor is: (n)p - If you use the p command to paste the code without entering a number before, it means pasting it once in the line next to the line where the cursor is.

  • Undo the previous operation: u – The modifications saved by entering w in bottom row mode can still be undone.

  • Undo the last undo: ctrl+r – Changes saved by entering w in bottom row mode can still be undone.

  • Cut several lines of code where the current line is located: (n)dd – The cut lines of code can (n)pbe pasted using commands. Cutting without pasting is equivalent to a deletion operation.

  • Jump to the last line of text: shift+g / G

  • Jump to the first line of text: gg

  • Jump to a specified line of text: n shift+g / n G

  • Jump to the beginning of the current line of text: shift+$

  • Jump to the end of the current line of text: shift+^

  • Jump forward once divided by words: b

  • Jump backward once divided by words: w

  • Fast case switching: shift+~ – After pressing once, the character where the cursor is located will be switched between upper and lower case, and the cursor will automatically move to the next position.

  • Replace several characters including the cursor position with the specified characters: (n)r 要替换的指定字符

  • Delete several characters including the cursor and the cursor to the right: (n)x

  • Delete several characters including the cursor and the cursor to the left: (n)X

  • Cursor position movement: h (left) j (bottom) k (top) l (right)

vim simple configuration

  • Under the directory /etc/, there is a file named vimrc. This is a public configuration file in the system and is valid for all users.

  • In each user's home directory /home/xxx, you can create a private configuration file named ".vimrc". This is the user's private configuration file and is only valid for that user.

For example, after ordinary users create a ".vimrc" file in their home directory, enter the set nu command in the file and save it, the line number will be automatically displayed the next time they open vim.

The configuration of vim is relatively complicated, and some vim configurations require the use of plug-ins. It is recommended not to configure them one by one by yourself. The simpler method is to directly execute the following command (execute this command under the user you want to make the vim configuration take effect. It is not recommended to execute it directly under root): curl -sLf
https://gitee.com/HGtz2222 /VimForCpp/raw/master/install.sh -o ./install.sh && bash ./install.sh

Then enter the root password as prompted:

Then wait for the installation configuration, and finally manually execute source ~/.bashrc.

image-20230728204036234

After the configuration is completed, functions such as automatic completion, line number display, and automatic indentation will be available.

3. Linux compiler gcc/g++

gcc/g++ are compilers used to compile C language and C++ respectively. The usage methods (instructions) of gcc and g++ are the same.

Complete compilation of program

gcc [-o 要生成的文件名] 要编译的文件

Note: gcc 文件名 You can complete the compilation of the file and generate an executable program named a.out in the current directory. Add -o to specify the file name of the generated program.

image-20230729154321844

Distributed compilation of programs

The complete process of program compilation:

  1. preprocessing
  2. compile
  3. compilation
  4. Link

Preprocess files

gcc -E 要处理的文件名 [-o 要生成的文件名]

Note: It is recommended to name the generated file with the .i suffix.

Preprocess the test.c file to generate the test.i file:

image-20230729154739105

test.i is preprocessed by test.c, so the header file of test.c will be expanded to increase the number of lines of code, and macro replacement, conditional compilation, and comments will be removed, so in the test.i file No macro definitions, conditional compilation and comments can be seen. At this time, the file content is still in C language.

Compile the file

gcc -S 要处理的文件名 [-o 要生成的文件名]

Note: It is recommended to name the generated file with a .s suffix. You can operate on the preprocessing result file or the source file.

Compile the test.i file obtained by preprocessing to obtain the test.s file:

image-20230729155519168

test.s is compiled from test.i. During compilation, the C language code is converted into assembly language.

Assemble files

gcc -c 要处理的文件名 [-o 要生成的文件名]

Note: It is recommended to name the generated file with the .o suffix. You can operate on the preprocessing result file, the compiled result file, and the source file.

Assemble the test.s file obtained by compilation to obtain the test.o file:

image-20230729160202816

test.o is compiled from test.s. During assembly, the compiled file will be converted into non-executable binary code.

Link files

There are no special command options for linking files, only instructions for complete compilation of the program are used. The Linux system has a built-in C language library, and the compiler will automatically complete the linking process.

Check the libraries used by the executable program to connect

ldd 可执行程序文件名

Use ldd to view the libraries connected to the executable program:

image-20230729160943100

Dynamic and static libraries

The libraries linked by the compiler are divided into static libraries and dynamic libraries .

Static library: When linking a static library, the corresponding code in the static library will be copied to the executable program. Therefore, the program linked to the static library takes up a larger space. However, if the static library disappears and does not affect the execution of the program, the static library generally With .a as the suffix.

Dynamic library: When linking a dynamic library, the address of the corresponding code in the dynamic library will be copied to the executable program. Therefore, the dynamically linked program takes up less memory. However, if the static library disappears, the program will not be able to execute normally. The dynamic library Generally, the suffix is ​​.so.

Static library:

Advantages: Does not rely on libraries, can be executed independently, and the program has high portability.

Disadvantages: takes up a lot of space.

Dynamic library:

Advantages: It takes up less space, and multiple programs can use the code of the dynamic shared library together.

Disadvantages: Depends on libraries, cannot be executed independently, and program portability is low.

Check the link status of the program through the file command:

image-20230729165622754

You can see that the mytest executable program uses dynamic linking and shared libraries.

gcc [-o 要生成的文件名] 要编译的文件 -static

Note: If the C language static library is not installed, you can execute it yum install -y glibc-staticto install it. You can also execute yum install -y libstdc++-staticthe C++ static library to install it.

When compiling with gcc, add the -static command option to obtain a statically linked program:

image-20230729165909556

You can see that mytest-static uses static linking, and the file size is larger than the dynamically linked mytest executable program.

4. Linux project automated build tool-Makefile

Makefile files are tools that can implement automated compilation functions.

Note: The Makefile needs to be created in the directory of the source file to be compiled.

Makefile composition

Makefile is an automated compilation tool composed of dependencies and dependency methods.

Dependencies

Dependency is a description of the relationship between a target file and a source file.

Dependency method

Dependency method is used to indicate the execution method of obtaining the target file through the source file.

Create a test.c source file and Makefile ( makefile ) file in the directory :

image-20230729182028699

In the Makefile, establish the following dependencies and dependency methods for the test.c file:

image-20230729183004157

After writing the Makefile, enter the make command to execute the Makefile:

image-20230729182534882

After executing the make command, the dependencies will be automatically viewed in the Makefile for automated compilation. Since mytest depends on test.o, test.o depends on test.s, test.s depends on test.i, and test.i depends on test. .c dependencies, so the dependency methods executed are in the reverse order of the Makefile dependencies.

.PHONY keyword

The .PHONY keyword is used by the Makefile to identify the pseudo target file so that the dependent method corresponding to the dependency relationship can always be completed.

Execute the make command multiple times on a Makefile:

image-20230729184414851

Usually when the make command is executed multiple times, only the first execution will be successful. When executed subsequently, an error message will be reported that the file is already the latest. At this time, you only need to use the .PHONY keyword to identify the dependency to be completed. The dependency method corresponding to the relationship is always completed. Modify the Makefile to the following content:

image-20230729184648767

make can be executed multiple times in succession without restriction:

image-20230729184743721

Usually the .PHONY keyword is used for cleaning target files:

image-20230729184943944

Execute the make clean command and specify the dependency method corresponding to the dependency method for executing clean:

image-20230729185459003

illustrate:

  • The source file of a dependency can be empty.
  • When executing the make command, only the dependency method corresponding to the first dependency scanned from top to bottom in the Makefile will be completed. (If the source files in the first dependency come from other dependencies, they will be executed together)
  • When executing the make command, you can specify the dependent methods to be executed.

The reason why make command recognition cannot be executed multiple times

The make command identifies that it cannot be executed multiple times by comparing the last modification time of the source file with the time when the executable program was generated, as follows:

image-20230729191517204

  • The last modification time of the source file is earlier than the time when the executable program was generated – the make command will not be executed repeatedly
  • The source file was last modified later than the time the executable program was generated – the make command can be executed

Check the creation and modification time of the file through the stat command:

Touching an existing file can modify the modification time of the file:

image-20230729194619872

all keyword

all is a commonly used keyword used to define a target named "all". The all target is usually used as the default target, that is, when executing the make command, if no target is specified, the all target will be executed by default.

In order to better explain the role of the all keyword, prepare the following source files test1.c and test2.c with the following contents:

image-20230802151058346

Create a makefile with the following contents:

all:mytest1 mytest2
mytest1:test1.c
	gcc -o mytest1 test1.c
mytest2:test2.c
	gcc -o mytest2 test2.c

.PHONY:clean
clean:
	rm -f mytest1 mytest2

Enter the make command:

image-20230802151312229

Since the dependencies of the all target require mytest1 and mytest2, the make command automatically searches for the dependencies and dependent methods of mytest1 and mytest2 for execution, completing the one-time automated compilation of multiple files.

Enter make clean to clean multiple programs at once:

image-20230802151516235

5. Linux debugger-gdb

gdb is the debugger under Linux system.

  • The program debugged by gdb can only be the debug version.
  • The binary program produced by gcc/g++ is in release mode by default. To use gdb for debugging, you must add the -g option when generating the binary program from the source code.

To facilitate debugging, prepare the following code:

#include <stdio.h>

int AddToTop(int top)
{
    
    
  printf("enter the func\n");
  int sum = 0;
  for (int i=0; i <= top; i++)
  {
    
    
    sum += i;
  }
  printf("quit the func\n");
  return sum;
}

int main()
{
    
    
  int top = 100;
  printf("the sum is %d\n", AddToTop(top));
  return 0;
}

Prepare the makefile file with the following content:

mytest:test.c
	gcc -o mytest test.c -g -std=c99
.PHONY:clean
clean:
	rm -f mytest

illustrate:

  • Since the code uses variables defined within a loop, it is necessary to indicate the use of the C99 standard during compilation to avoid compilation failure.
  • Use the -g option to make the resulting binary program a debug version.

After preparing the code and makefile files, compile them to form an executable program. To ensure that the executable program has debugging information, use readelf - S mytest | grep debugcheck:

image-20230802121818005

Use gdb to start debugging the program:

image-20230802121955983

View code command: l(list) + number--display the code starting from the corresponding line.

Show code starting from the first line:

image-20230802122552997

Since gdb will record the last command, enter and press Enter to continue displaying the following code:

image-20230802122627028

Breakpoint command: b(breakpoint) + number/source file: function name – Put a breakpoint on the corresponding line of code.

Put a breakpoint on line 18 of the code:

image-20230802122830696

View existing breakpoint instructions: info b(breakpoint).

image-20230802122957922

Some information:

  • Num – breakpoint number.
  • What – Indicates the source file and location of the breakpoint.

Run program instructions: r(run) - If there is a breakpoint, it will run to the breakpoint and stop. If there is no breakpoint, it will run to the end of the program.

Run the program to the breakpoint:

Check the breakpoint information again. The breakpoint information records that the breakpoint was hit once:

image-20230802123551646

Delete breakpoint command: d(delete) + breakpoint number – delete the breakpoint corresponding to the breakpoint number.

Delete the breakpoint with breakpoint number 1:

image-20230802123840597

Turn off breakpoint command: disable breakpoint breakpoint number.

Turn off breakpoint number 2:

image-20230802124410058

Note: The Enb information column indicates that the breakpoint is closed, and y indicates that the breakpoint is open.

Enable breakpoint command: enable breakpoint breakpoint number.

Start breakpoint number 2:

image-20230802124551179

Execute code instructions step by step: n(next).

After run executes the program, enter next to execute the code step by step:

image-20230802135656755

Execute code instructions statement by statement: s(step).

After run executes the program, enter s to execute the code statement by statement:

image-20230802135754713

You can view the function call structure through the bt command:

image-20230802140115556

Monitor variable command: p/display variable name – p means display once, display means permanent display, undisplay+number cancels permanent display.

Monitor the value of a variable:

image-20230802140502040

Cancel the permanent display numbered 1:

image-20230802140838468

Jump code instructions: until + number – In the function body, jump the code to the corresponding line.

Jump to line 10 in the AddToTop function:

image-20230802141517544

Complete the execution instructions of the current function: finish - directly complete the execution of the current function.

Complete the execution of the AddToTop function directly and get the return value:

image-20230802141731958

Jump to the next breakpoint location instruction: c(continue).

image-20230802142551214

Exit gdb command: quit.

Guess you like

Origin blog.csdn.net/csdn_myhome/article/details/132063646