Simple Usage of Makefile and GDB

Simple Usage of Makefile and GDB

1. Why Use Makefile for Compiling Multiple .C Files:

(1). Generally, when we are developing a project, we will create a project with many directories, and each directory contains many .C files, which requires joint compilation.

(2). If we compile manually, the multiple program files are intertwined, leading to dependencies and dependent relationships between different files. Thus, during compilation, issues will arise: we must resolve dependencies before addressing the dependent items.

In this case, manual compilation becomes exceptionally difficult. We must remember the dependency relationships to understand the compilation order, and each time we have to execute many gcc compilation commands to achieve the final compilation target (gcc also has many parameters).

(3). This is when we need to use Makefile. Makefile is an effective tool for project management, written in a scripting language.

2. Rules for Using Makefile:

Command: make test

./test (test is the target file you generate)

make: only modifies the files you have changed

Writing rules in Makefile:

target… : [dependencies…]

[@]command

(1). The [] content is optional.

(2). The @ symbol: a suppressor for command output in the terminal.

(3). There must be a tab character before the command.

Variables can be defined in Makefile, which helps reduce writing rules.

Variable usage format: ${variable_name}.

Automatic variables:

Command

Explanation

$@

Represents the target file in the rule

$^

Represents all dependency files in the rule

$<

Represents the first dependency file in the rule

%.o : %.c

All .o files correspond one-to-one to their following .c files (pattern rule)

Note: (1). If .PHONY:clean appears in Makefile, it means that if there is also a clean file in the current directory, the command clean in the Makefile will be executed.

(2). When there are defined rules, the commands for those rules will be executed.

3. Some Knowledge of Header Files

The difference between < > and ” “:

a. < >, looks in the system’s specified directory, generally in /usr/include/xxx.h

b. ” “, looks in the current directory

Header file format:

#ifndef _TEST_H_

#define _TEST_H_

……

#endif Conditional precompilation purpose: to prevent repeated inclusion of header files.

4. Using GDB

(1). Command: gcc -g test.c -o test (-g is a required parameter for debugging)

gdb -q test (-q skips prompts and goes straight to debugging) (test is the target file to debug)

start Enter debugging

s and n: s indicates entering a function when encountered, debugging inside the function; n indicates not entering the function, continuing to debug.

(2). Set breakpoints b line_number

run: This will jump to that line for execution; delete breakpoint d n (n is not a line number, indicating which breakpoint to delete)

Check breakpoints info b

(3). Some output:

Print/format corresponding quantities

p/x str p/c *str p/d str …… can view corresponding addresses, values, characters……

Exit gdb debugging: q

Recommended Reading:

Linux File Attributes and User Groups

Linux User Environment

UNIX Kernel Source Code – Generic Linked List

Learn Technology Together

Simple Usage of Makefile and GDB

Scan the QR code to follow my official account

Leave a Comment