Linux System Programming II -- Makefile and gdb debugging

1, Makefile

1. What is Makefile

There are countless source files in a project, which are placed in several directories according to type, function and module. Makefile file defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, which files need to be recompiled, and even more complex function operations, because makefile file is like a Shell script, You can also run commands from the operating system. The benefit of makefile is "automatic compilation". Once written, only one make command is needed, and the whole project is fully compiled automatically, which greatly improves the efficiency of software development. Make is a command tool that explains the instructions in makefile files. Generally speaking, most ides have this command, such as make in Delphi, nmake in Visual C + +, and make in GNU under Linux.

2. Naming rules of makefile

2.1 document naming

Makefile or makefile. After entering the make command, the operating system will find the above two files in the current path for compilation

2.2 Makefile rules

1. Other rules in makefile generally serve the first rule

2. A Makefile can have multiple rules in the following form:

Target........: dependent file

Command (Shell command)

      .........

3. Before executing the command, you need to check whether the dependency in the rule exists

- execute if present

- if it does not exist, look down to see if there is a rule to generate this dependency

4. Detect the update. When executing the command in the rule, it will compare the time of the target and dependent files

- if the dependency is updated late, the target needs to be regenerated

- if the target update time is late, there is no need to execute the command

2.3 variables in makefile

Variable:

//Custom variable
//Variable name=Variable value
var=hello

//$Get variable value
$(var)

// Predefined variables 
AR //Archive maintenance program name
CC //C Compiler name, default to cc
CXX //C++Compiler name, default to g++
$@ //Full name of the target
$< //Name of the first dependent file
$^ //All dependent files

//give an example
app : main.c a.c b.c
    gcc -c main.c a.c b.c -o app

//Variable can be replaced with
app : main.c a.c b.c
    $(CC) -c &^ -o $@

Pattern matching:

    %.o : %.c

-%: wildcard, matching the same string

2.4 functions in makefile

  $(wildcard PATTERN...)

- function: obtain the list of files of the specified type under the specified directory

- parameter: PATTERN refers to a certain type of file corresponding to multiple directories, separated by spaces

- return value: get several file lists separated by spaces

- example:

    $(wildcard *.c ./sub/*.c)

Return: A.C, B.C, C.C, D.C

  

  $(patsubst <pattern>,<replacement>,<text>)

- function: find out whether the words in < text > (separated by "space", "Tab" or "carriage return" and "line feed") conform to the pattern < pattern >, and if they match, replace them with < replacement >.

- < pattern > can include a wildcard '8' to represent a string of any length. If < replacement > also contains' 8 ', then the `%' in < replacement > will be the string represented by the% in < pattern > (you can escape with ` \ 'and use ` \%' to represent the real `% 'character)

- return value: the function returns the replaced string

- example:

    $(patsubst %.c, %.o, x.c bar.c)

Return: x.o bar.o

2, GDB debugging

1. What is GDB

GDB is a debugging tool provided by GNU software system community and forms a complete development environment together with GCC. GDB is the standard development environment in Linux and many Unix like systems. - generally speaking, GDB mainly helps you complete the following four functions:

1. Start the program and run the program at will according to the customized requirements

2. The debugged program can stop at the specified set breakpoint (the breakpoint can be a conditional expression)

3. When the program is stopped, you can check what happens in the program at this time

4. The program can be changed to correct the impact of one BUG to test other bugs

2. Start preparations for GDB commissioning

Usually, when compiling for debugging, we will () turn off the compiler's optimization option (` - o ') and turn on the debugging option (` - g'). In addition, the ` - wall 'option turns on all warning s without affecting the program behavior as much as possible. You can also find many problems and avoid some unnecessary bugs.

gcc -g -wall program.c -o program

The '- g' option is used to add source code information to the executable file, such as the lines of the source code corresponding to the first machine instruction in the executable file, but it does not embed the entire source file into the executable file. Therefore, gdb must be able to find the source file during debugging.

3.GDB common commands and operations

  

 1 gdb  [file] //Start on a file with debug information gdb debugging
 2 quit //Exit debugging
 3 set args .. .. .. //Setting parameters for the debugger
 4 show args //Get parameters
 5 show list/listsize //Gets the number of rows currently displayed
 6 set list/listsize Number of rows //Sets the number of rows displayed each time
 7 set inferior-tty process id//Set debugging for different processes
 8 info inferiors //Displays all processes and that currently exist id
 9 show nferior-tty //Display current debugging process
10 
11 l/list //Display from default location
12 l/list Line number //Displays the specified row in the middle
13 l/list Function name //Displays the specified function in the middle
14 
15 //View codes that are not current files
16 list/l file name:Line number 
17 list/ l file name:Function name
18 
19 
20 //Set breakpoint
21 b/break Line number 
22 b/break Function name
23 b/break Files: line numbers
24 b/break Files: function names
25 
26 i/info b/break //info break 
27 d/del/delete Breakpoint number//Remove Breakpoint 
28 dis/disable Breakpoint number //Setting breakpoint is invalid
29 ena/enable Breakpoint number //Set breakpoint to take effect
30 b/break Breakpoint number if condition //Set conditional breakpoints
31 
32 //Debugging instruction
33 //function
34 start //The program stops on the first line
35 run //Stop only when a breakpoint is encountered
36 
37 //Continue running and stop at the next breakpoint
38 c/continue 
39 
40 //Execute one line of code down without entering the function
41 n/next
42 
43 //Step down debugging
44 s/step
45 
46 //Jump out of function body
47 finish
48 
49 //Variable operation
50 p/print Variable name //Print variable values
51 ptype Variable name //Print variable type
52 display Variable name //Automatically print specified variables
53 i/info display //View automatic variable information
54 undisplay number //Delete automatic variable
55 
56 //Cyclic correlation
57 set var Variable name=Variable value
58 until //Jump out of the loop (provided there is no breakpoint in the loop)

 

Tags: Linux

Posted on Sat, 04 Dec 2021 23:02:18 -0500 by Donovan