This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.


Package manager Yum

What is a software package

To install software under Linux, the usual method is to download the source code of the program, compile it, and get an executable program. But this is troublesome, not easy to operate, so some people put the commonly used software compiled in advance, made into a software package (can be understood as the Installation package on Windows) on a server, through the package manager can be very convenient to obtain the compiled software package, directly install.

Software packages and package managers are like “App” and “App Store “.

Yum is a popular package manager for Linux. It is mainly applied to Fedora, RedHat, and Centos distributions.


2.ping

Since yum obtains the installation package from the server, all operations related to yum must be performed to ensure that the host (VM) network is normal. The network connection can be verified by using the ping command.


3. View all software packages

You can see all of the packages through the Yum List (there are many packages, and the following image only captures some of them).

You can search for the specified software package together with the grep command.


4. Install software packages

Sudo yum install XXX can be used to install XXX software. This section uses LRZSZ as an example.


5. Uninstall the software package

Sudo yum remove XXX you can uninstall the XXX software package.


Ii. Editor Vim

0. Vim configuration

Native Vim is not easy to use and needs to be configured to make it easier to use.

(1) Configuration file

Under the /etc/directory, there is a file called vimrc, which is the common VIm configuration file on the system and is available to all users. In the home directory of each user, you can create a private configuration file named.vimrc. The vim configured here does not affect other users.

Enter the user’s main working directory, find the. Vimrc file in your own directory, and execute vim. vimrc to configure your own VIm.


(2) Configure viM

Enter special commands to modify vim’s options, which can be tested with the following three commands.

Set syntax highlighting: syntax on display line number: set nu Sets indent space to 4: set Shiftwidth =4


Execute the following command on the shell command line (execute this command on whichever user you want the VIm configuration to take effect. It is strongly “not recommended” to execute directly under root), follow the prompts to turn vim into a c++ compiler.

The curl – sLf gitee.com/HGtz2222/Vi… -o ./install.sh && bash ./install.sh

(original address: gitee.com/HGtz2222/Vi…


1. Vim model

Vim has many modes, seven basic and six additional modes on the server I use.

This article introduces only three common modes: Normal mode, Insert mode, and Last Line mode.


2. Transitions between modes

The previous diagram shows the relationship between the three modes.


Vim opens in command mode by default, as shown below.

At this time, if we input general code, there may be no response, because in the command mode, VIM treats the input content as a command. If we input according to the code, there will be no command, so there is no response most of the time.

At this point, if you press “I”, you can enter the insert mode. The lower left corner will look like the picture below, indicating that you have entered the insert mode. At this time, you can enter the content in the editor.

Press Esc to return to normal/command mode.

If you want to exit vim, do it in bottom line mode (shift+; That is, enter a 🙂 and enter q to exit. Type w to save, butThe configured Vim editorWill automatically save, so w can be omitted; If you are using native Vim (not configured), you need to manually save.


3. Command mode (Normal mode)

(1) Conversion from command mode to insert mode

To switch from command mode to insert mode, enter: a (to enter insert mode after moving the cursor back one character), I (to enter insert mode directly), o (to enter insert mode after a line break).


(2) Common editing commands in command mode

Shift +^: Cursor at the beginning of the current line Shift +$: cursor at the end of the current line Shift + G: cursor at the last position of the current text gg: cursor at the first position of the current text N + Shift + G: cursor at the beginning of the NTH line YY: copy the line where the cursor is N +yy: Starting from the line where the cursor is, copy the following n lines p: Paste the content N + P: Paste the content N lines U: Undo CTRL + R: N + DD: Starting with the cursor, delete the following n lines shift+~: Change the case of the cursor position w: jump in units of words Skip to the next to the beginning of the "word" (from left to right, from top to bottom) b: in "word" jump, jump on the beginning of a "word" (from right to left, from bottom to top) e: in "word" jump, jump to the next at the end of the "word" (from left to right, from top to bottom) (the "words" may be a group of letters, N +x: delete n positions from the cursor shift+x: delete x positions before the cursor (excluding the cursor position) Shift + N +x: Delete x positions before the cursor (excluding the cursor position) N +r: Replace the contents at n positions starting from the cursor (the contents at each position are the same) Shift + R: Enter the replacement mode. H, j, k, L: left, down, up, right (same function as arrow keys, but it is recommended to use letters instead of arrow keys, CTRL + B: scroll down a page CTRL + F: scroll up a page CTRL + U: scroll down half a page CTRL + D: scroll up half a pageCopy the code

It is highly efficient to edit text in command mode. Therefore, you are advised to edit text in command mode.


4. Insert mode

Command mode Press A/I/O to enter the insert mode. The insert mode is the same as normal text editing on Windows, and will not be described here.


5. Last Line Mode

Command mode shift+; To enter the bottom mode, run the following commands:

setNu: Sets the line numbersetNonu: cancel line number w: write (save) w! : Forcibly write (or fail to write) q: exit q! : Forcibly exit! + Other commands: In bottom line mode, type! Then you can execute ls,ll, GCC and other commands, that is, you can see the command line without exiting vimCopy the code

Vs command +filename: Displays a split screen. If filename does not exist, a new screen is created. CTRL + W + W is available in themultipleToggle between tabs.

Note that there are not just two split screens, but many split screens.

When exiting split screen in bottom line mode, exit the same file as the cursor.


GCC /g++

GCC is used to compile C files and g++ is used to compile C ++ files. The options are almost the same.

1. From source files to executable files

There are four steps to go from a.c source file to an.exe executable.

(1) Pretreatment (-E)

Mainly include: header file expansion, uncomment, macro replacement, conditional compilation. In Linux, you can use GCC’s -e option to stop source files after preprocessing and generate.i files with the same name.


(2) Compile (-s)

Check the standardization of the code, whether there are grammatical errors, etc., to determine the actual work of the code to do, after the check is correct, translate the C language code into assembly language. In Linux, you can use GCC’s -s option to stop the source file after compilation and generate an.s file with the same name.


(3) Compilation (-C)

Generate machine-recognized code to convert the.s file generated in the compilation stage into a.o file. After the conversion, the file is a binary object file. In Linux, you can use the -c option of GCC to stop the source file after assembly and generate a.o file with the same name.


(4) Link (no option)

Associate calls to library functions in code with implementations of library functions in the library.

Here to introduce a concept: function library function library is generally divided into static library and dynamic library two kinds.

Static library

When linking, all the code of the library file is added to the executable file, so the generated file occupies a relatively large space, but the library file is no longer needed at run time, so the portability of the program is strong. The static library suffix is usually.a.

The dynamic library

Instead of adding the code of the library file into the executable file, the library is loaded by the link file when the program is executed. The generated file is small and fast, but poor portability. Dynamic libraries usually have the suffix.so.

GCC uses dynamic libraries by default when linking.


2. Common options

In addition to the -e, -s, and -c options above, there are some common options below.

-o0, -o1, -O2, and -O3: indicates four levels of compiler optimization options. -o0 indicates that no optimization is performed. -O3 indicates the highest optimization level. -Wall: generates all warning information. -o+filename: Names the generated file as filename


4. Debugger GDB

Here is a simple code increment from 1 to 100 to demonstrate debugging. The executable generated from the source file is Mytest.

The code is as follows:


GDB + the debugged file name is displayed as followsHowever, no debugging information is displayed. In Centos 7 GCC, the executable generated by default is a release version and cannot be debugged. Make the program debug by adding the -g option at compile time.

1. Display the code list/ L



2. Run the r


3. Breakpoint B. View breakpoint info and delete breakpoint D


4. Process by process N (do not enter function), statement by statement S (enter function)


5. Temporarily check the variable value p


6. Add display


7. Uncheck undisplay


8. Jump until


9. Finish the function


10. Go to the next breakpoint C


11. Exit debugging quit

Make +makefile

1. The background

The source files in a project are not counted. They are placed in several directories by type, function, and module. Makefiles define a set of rules to specify which files need to be compiled first, which files need to be postcompiled, which files need to be recompiled, and even more complex functions. Once written, only one make command is required. The entire project is automatically compiled, greatly improving the efficiency of software development. Make is a command and makefile is a file, both of which are used together to automate the construction of projects


2. Dependency relationship and dependency method

Examples are as follows:


One line in the Makefile is a dependency, followed by a dependency method. The dependencies from a.c file to an.exe file are described below.

O/test.s/test.s/test. I/gcc-s/test. I/test.c/gcc-e (Left side of colon is obtained file, right side of colon is dependent file)

After make, you can get all the files you need.

While these processes can be done in one step with GCC in this scenario, if you have multiple files that need to be processed, the efficiency can be greatly improved when you write so many lines of instructions into a makefile and then execute them all with a single make.


3. Project clearance

As you can see, there are a lot of files that can be very cumbersome to clean up one by one. Add the clean option to the makefile to clean up some files just like make does. PHONY is a PHONY target, so clean is always executed.


4. The macro substitution

$@ : Target file in dependency (file to the left of colon)

$^ : List of dependent files in dependencies (all files to the right of the colon)

$< : a dependency file in the dependency

Modify the Makefile to the result of the following macro substitution, so that the dependency method does not need to change when the dependency file or target file changes.

It still works normally.


Thank you for reading. If there are any mistakes, please correct them