Linux system (2) - development tools in the Linux environment

        Following the previous blog, the use of commonly used tools such as vim editor, gcc toolchain, makefile and gdb in Linux environment is rationalized.

1. vim editor

1. Working mode

        vim is a common text editor for Linux. vim has two basic working modes:

        Command mode : The entered characters are used as commands and do not enter the edited document.

        Edit Mode : The characters entered will modify the content of the document.

        In the command mode, if you enter some editing commands (usually switch to the edit mode where the insertion position is at the cursor, type i ), you will switch to the edit mode; in the edit mode, press Esc to switch back to the command mode.

 There are three command modes: normal mode, command-line mode and selection mode.

The normal mode is executed after the input is completed, no need to enter to confirm. Enter a colon in normal mode to enter command line mode .

Command line mode: Enter the command and press Enter to confirm before execution, and return to normal mode after execution.

(1) In normal mode, commands are divided into moving cursor, scrolling screen, editing, etc.

(a) Move cursor class:

(b) Scroll screen class: ctrl + F scroll down one screen ctrl + B scroll up one screen Ctrl + U scroll up half screen ctrl + D scroll down half screen

(c) Edit class:

(d) File type: ZZ save and exit

 In normal mode, enter the lowercase letter v to enter the selection mode , enter the uppercase letter V to enter the row selection mode , and press the Ctrl+V key combination to enter the column selection mode .

(2) Command line mode

:help View the help page :0 Move to the first line :¥Move to the last line :n Move to the nth line :wSave the file :qQuit the file :nCreate a new file: wqSave and exit

Second, the gcc tool chain

A commonly used compilation tool on GNU/Linux systems is GCC. GCC is not essentially a single program, but a collection of multiple programs, so it is often referred to as a toolchain.

  1. Program compilation process:

(a) Preprocessing

At this stage, all prepared statements in the source code are processed, such as

The contents of the file included in the #include statement replace the statement itself

All defined macros are expanded

According to whether the conditions of statements such as #ifdef #if are established, the corresponding part is selected

The gcc preprocessing stage can generate .i files , and the option -E can make the editor stop compiling at the end of the preprocessing . E.g:

gcc -E -o  hello.i  hello.c

(2) Compile

At this stage, the compiler performs syntax analysis, optimization and other operations on the source code, and finally generates assembly code. This is the most important part of the whole process.

The option -S can be used to make GCC stop after compiling and generate .s assembler . E.g:

gcc -S -o hello.s hello.c   

(3) Assembly

In this stage, the assembler is used to process the assembly code, generate machine language code, and save it as an object file with a suffix of .o .

When the program consists of multiple code files, each file must be assembled, and the next step of linking work can only be performed after the .o object file is generated.

The object file is already part of the final program, it just cannot be executed until it is linked. Object files can be generated with the -c option:

gcc -c -o hello.o hello.c    

(4) Links

The assembled machine code cannot be run directly. In order for the operating system to correctly load the executable file, the file must contain a fixed-format header. It must also be linked with the system-provided startup code for proper operation, which is done by the linker.

gcc -o hello hello.c   

2. Basic usage of gcc

gcc [-Wall] [-O1..3] [-o name] file

-Wall: turn on all warnings

-O sets the optimization level, O0 means to turn off the optimization function

-g compile debug information into object file

-o name: Specifies the name of the output file is name

file: The file to be compiled (linked).

Example of use:

Three, make sum makefile

 A software usually contains many files, and compiling it every time is cumbersome and inefficient.

VC and VB on the Windows platform have project concepts. Once a project is established, these development platforms will automatically maintain various files in it, so as to compile efficiently.

In Linux, this is done using make.

1, makefile file description

To use the make tool, you must first write a makefile. A makefile contains 5 aspects:

1. Specific rules 2. Implicit rules 3. Define variables 4. Instructions 5. Comments 

2, the rules of makefile:

Tools such as Autoconf and Automake can automatically generate makefiles. The core of the content of the makefile is a series of rules. The basic format of a rule is:

target:dependency

       (tab character) command

(1) target (target): usually the name of the file to be generated

(2) dependency (dependency): refers to the file used to input and generate the target.

(3) command (command): It is the action executed by make, a rule can have multiple commands, each command is one line. The first character of the command line is TAB

3. Examples of makefiles:

(1) Example 1:

objects = main.o mouse.o command.o display.o

CC = gcc

CFLAGS = -Wall -O2 -g //equivalent to defining variables in C

game : $(objects) //Define game generation rules

             $(CC) -o edit $(objects)   

main.o : defs.h //Define main.o generation rules, including implicit rules

mouse.o : defs.h command.h

command.o : defs.h command.h

display.o : defs.h buffer.h

.PHONY : clean

clean : //Define the rules for clean, no dependencies

          $(RM) edit $(objects)  

  The generation rules of the game include three basic elements of the rules: target, dependency, and command ; rules such as main.o have no commands and belong to implicit rules. The clean rule has no dependencies, it does not belong to the compiled content, it just completes a specified action.      

Implicit rules tell make to use traditional standard methods to accomplish tasks. For example, the method of generating an object file is to use a C compiler to compile the C language source program , and the commands used in this step are basically the same. The use of implicit rules eliminates the need to specify these commands in detail, and make can determine the rules to be used according to the change of the suffix of the file name .      

(2) Example 2:

A variable is a name defined in a makefile to replace a text string called the variable's value. Common predefined variables in Makefile are:

(a) CC The default cc (gcc) is the name of the C compiler.

(b) CPP defaults to $(CC) -E is the name of the C and precompiler.

(c) The default value of CXX is g++, which is the name of the C++ compiler,

(d) RM The default value of rm -f is the name of the removal program.

(f) $@ is used in the generation rule to indicate the current target.

(g) $< is used in build rules to represent the first dependent target of the current target.

(h) $^ is used in build rules to represent all dependent targets of the current target.

How to use variables: $ (variable name) When the variable name is only a character, () can be omitted

Commonly used variable assignment operations:

:= Direct assignment, the letter value overwrites the original value.

? = Conditional assignment, if the original has no value, the assignment is made. Otherwise keep the original value.

+= Additive assignment, the new value is appended to the original value.

= Recursive assignment, if the right side contains other variables, when the value of these variables changes, the value of the assigned variable also changes.

4, the usage of make:

The basic usage of the make tool is as follows:

make [-C dir] [-f file] [target]

-C dir: enter the dir directory when executing, the default current directory

-f file: use file as makefile

target: The target to be completed, the target is defined in the makefile, the default is the first target defined.

Fourth, gdb debugging tools

GDB is a GNU debugging tool, it can trace the program being debugged, set breakpoints, single-step execution and other operations. When the program execution is suspended, you can use commands to view the variable values, CPU register values, memory values, and function call stacks and other information in the program.    

The application being debugged should preferably be compiled with the -g parameter to include debugging information into the object file:

gcc -g app.c -o app

Start debugging the program app with the following command:

gdb app

If the program app requires parameters when running, use the following command:

gdb --args app arg1 arg2 // arg1 and arg2 are treated as arguments to app, not to gdb  

After startup, enter the GDB interactive interface, and you can enter GDB commands to debug. The commonly used commands are as follows (Enter to repeat the previous command):

 

The advanced part of Shell looks a bit complicated, and it is not easy to get it done. You need to take some time to go over it carefully. That's it for today. It can be considered that today's goal has been completed. To sum up today: I am a bit playful during the day and waste a part of the time. In the future, try to arrange the entertainment time after 9 o'clock in the evening. Another aspect is that you need to plan your exercise time. Today, I just walked a few laps outside after dinner. The amount of exercise is too little and needs to be strengthened! Tomorrow, I will go through all the grammars of the C language. These things should be memorized by heart. I will go through them quickly, check for deficiencies and fill omissions, and type some common and simple data structure examples according to the table to prepare for the day after tomorrow.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324355388&siteId=291194637