Linux3: Use of basic tools under Linux (vim editor, gcc compiler, gdb debugging tool, makefile compilation)

Linux3: Use of basic tools under Linux (vim editor, gcc compiler, gdb debugging tool, makefile compilation)

1. Use of vim editor:

installation:

sudo apt-get install vim

If you encounter:
Insert picture description here
Solution:

//强制解锁
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

After that, you can install it.

1.1vim editor:

vi/gedit: comes with the system.
vi/vim isUniversal file editor under linux, There is no menu, it has platform versatility, and it can also be run under windows.
vim has
four modes:Command mode Insert mode Bottom line mode Visual mode

Command mode : also known as normal mode. This mode mainly executes vim command operations. You can move the cursor to browse and delete the entire line, but you cannot edit the text. When you first open vim, it is in this mode by default. No matter what mode you are in, press the ESC key to enter this mode.

Insert mode : Normal text writing mode, press the command shortcut key to enter this mode in command mode. i,I,a,A,o,O.

Bottom line mode : In this mode, the cursor is located on the bottom line of the screen, and the content we input will appear on the lowest line. In the command mode, press the colon (:) to enter the bottom line mode.

Visual mode : select the text. Press v in the command mode to enter the visual mode.

1.2vim editor use:

Open a file called file1.txt with vim:
execute:
open vim file1.txt and jump to a certain line:
vim file1.txt + line number
common operations (with: indicates the bottom line mode operation command; without: indicates the command Mode), case-sensitive;
file save command ------ with a colon indicates the command in bottom line mode
: w save file
: w file1 save as file1
: wq save and exit vim
: q exit vim (not saving prompt Succeeded)
:q! Force exit (do not save, no prompt)
:x Same as :wq.
Cursor movement:
1. Direction key operation,
2. In command mode, h means to move the cursor to the left, j to move the cursor down, k to move the cursor up, and l to move the cursor to the right.
3.—vim After installing the plug-in, we can also use the mouse to operate.
Enter the insert mode command
i to enter the insert mode, insert the content before the cursor position
I enter the insert mode, insert the content at the beginning of the line where the cursor is
o Create a new line below the line where the cursor is One line, enter the insert mode,
O create a new line above the line where the cursor is, enter the insert mode,
a enter the insert mode, start inserting content after the cursor position
A enter the insert mode, start inserting the content at the end of the cursor line
Copy and paste command:- ---In command mode,
nyy means to copy the content of n lines starting from the line where the cursor is located, and omit n means to copy the line where the cursor is located.
np Paste, n represents the number of pasting, which can be omitted, which means once.
nP is pasted on the line where the cursor is.
y Copy the selected content.
"y Copy selected content, unlike y, it can be pasted into another file.
"p Paste" y copied content.

Text selection-Visual mode is dedicated to selecting text.
Insert mode-you can select text by moving the cursor

Text delete command ------
1) d0 delete to the beginning of the line
2) d$ or D delete to the end of the line
3) x delete a character after the cursor
4) X delete a character before the cursor
5) ndd delete the current line And then n-1 lines, omit n means to delete the line where the cursor is.
6) The last deleted content, execute p to paste the content
7) dw delete to the end of the current word

Text replacement and modification command:
move the cursor to the character to be modified, press r and then press the character to be modified to perform
a single character replacement

1.3 Vim editor enhanced version configuration
You can refer to the system programming book vim configuration, or decompress the compressed package in the documentation to the current user's home directory.

1.4 Graphical Editor
Graphical editing tools gedit and kedit under Linux. Basic configuration use (modify font size, tab tab)
shortcut key operation: ctrl +s save
Ctrl+q exit

2. The use of compiler

2.1 Program compilation process
Compiler preprocessing—compiler—assembler—link—generate executable program

Insert picture description here

2.2 Introduction to gcc compiler

1. # gcc source file

Generate executable file a.out (if the generated file name is not specified, default a.out)

2. Specify the name of the generated file:

# gcc -o 源文件 生成文件名

Pay attention to the order:
Thinking: Can it work if -o is written to the front? ? ? ?

# gcc -o 源文件 生成文件名 错误
# gcc -o 生成文件名 源文件 正确

Summary: -o option must be followed by the file to be generated
3. gcc shows the program compilation process
gcc -E source.c -o source.i
gcc -S source.i -o source.s
gcc -c source.s -o source .o
gcc source .o -o executable program

2.4 Library files
In order to improve the efficiency of program writing and the idea of ​​modular programming, we can package the corresponding functions into library files for use. The library
file under Linux is divided into static library and dynamic library file.
lib .a * The static library is linked into the executable file
lib .so during compilation . The dynamic library is only called during compilation, and the library is linked when the generated file is executed.

2.5 Static library
fun.c main.c
gcc -c fun.c Compile the file to generate a binary file
ar -rc libfun.a fun.o Package the binary file to generate a static library file.
gcc main.c libfun.a -o main Use library to compile files.
The static library packages the library files and source files to generate an executable program.This causes the executable program to be too large

2.6 Dynamic library
Dynamic library — .so file
gcc -c fun.c generates binary file
gcc -fpic -shared fun.o -o libfun.so
generates dynamic library file.
gcc main.c libfun.so -o main
ldd main Found that the dynamic library file could not be found.
cp libfun.so /lib

3.gdb debugging tool

3.1 Introduction to
gdb GDB is a powerful debugging tool for UNIX programs released by the GNU open source organization. Perhaps, you prefer the graphical interface method
, such as VC, BCB and other IDE debugging, but if you are doing software under the UNIX platform, you will find that GDB is a debugging tool that is better than the
graphical debugger of VC and BCB. powerful functions. The so-called "inches are long and feet are short" is this truth.
Generally speaking,GDB mainly helps you complete the following four functions:
1. Start your program, you can run the program as you want according to your custom requirements.
2. Allow the debugged program to stop at the breakpoint you specify. (Breakpoints can be conditional expressions)
3. When the program is stopped, you can check what happened in your program at this time.
4. Dynamically change the execution environment of your program

3.2 gdb use

Source program: main.c


#include <stdio.h>
int main()
{
    
    
int i=1;
printf("I am main!!!1\n");
i=2;
printf("I am main!!!2\n");
i=4;
printf("I am main!!!3\n");
i=6;
printf("I am main!!!4\n");
return 0;
}
1、gcc main.c -g//编译 main.c 生成可以调试的可执行程序 a.out
2、gdb a.out
l //显示全部代码
b n//在第 n 行设置断点
r //运行
p i//打印变量 i 的值
c //运行到最后
q //退出

4.make project management tool

4.1 Introduction to
make tool Imagine a project consisting of hundreds of files. If only a few files have been modified, it
would be a cumbersome process to recompile each file from beginning to end. For this reason, the concept of Make project manager is introduced. Project manager refers to the management of
more files. It is an automatic manager that can automatically find updated files according to file time and reduce the workload of compilation. At the same time, it can read in
Makefile files. To perform a lot of compilation work.

4.2 Compilation Rules
Goal: Dependent
Line feed should be treated as tab key.
E.g:

Insert picture description here

Reference tree:
Common tools under linux
Note: need to use xmind software to view

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108802210