Understanding the Simple Use of Makefile and gdb!

Learning technology requires a deep inquiry and tracing back to the source. For readers learning Linux C++, Makefile and gdb are essential!1.Why use Makefile for compiling multiple .C files?(1) Generally, when we develop a project, we create a project structure with many directories, each containing multiple .C files, which necessitates combined compilation.(2) If we compile manually, the interwoven program files will create dependencies and relationships between different files. This leads to issues during compilation: dependencies must be resolved before dependent items.In this case, manual compilation becomes exceptionally difficult. First, we must remember the dependency relationships to know the compilation order. Secondly, we need to execute numerous gcc compilation commands each time to achieve the final compilation target (gcc has many parameters as well).(3) This is where Makefile comes in. Makefile is an effective tool for project management, written in a scripting language.2.Rules for using MakefileCommand: make test./test (test is your final target file)make: only modifies the files you have changedWriting rules in Makefile:target… : [dependencies…][[@]command](1) The content in [] is optional.(2) The @ symbol: suppresses 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 to their respective .c files (pattern rule)

Note:(1) If .PHONY:clean appears in Makefile, it means that if there is a clean file in the current directory, the command clean in Makefile will be executed.(2) When there are specific rules, the commands for those rules will be executed.Understanding the Simple Use of Makefile and gdb!This is the Makefile content before using variables.Understanding the Simple Use of Makefile and gdb!This is the simplified Makefile after using variables.3.Some knowledge about header filesThe difference between < > and ” “:a. < > looks for files in the system’s specified directories, usually in /usr/include/xxx.hb. ” ” looks for files in the current directory.Header file format:#ifndef _TEST_H_#define _TEST_H_……#endif The purpose of conditional precompilation: to prevent multiple inclusions of the header file.4.Using gdb(1) Command: gcc -g test.c -o test (-g is a necessary parameter for debugging)gdb -q test (-q skips prompts and goes directly to debugging) (test is the target file to debug)start Enter debuggings and n: s means to enter the function when encountered, jumping into the function for debugging; n means to continue debugging without entering the function.(2) Set breakpoints b line_numberrun: This will jump to that line for execution;Delete breakpoint d n (n is not a line number, it indicates which breakpoint to delete)View breakpoints info b(3) Some outputs:Print/format corresponding quantitiesp/x str p/c *str p/d str …… can view corresponding addresses, values, characters……Exit gdb debugging: qRecommended Reading:

Simple Configuration of Linux Network

Linux Logs, Scheduled Tasks, Hostname, System Time!

Daemon Processes and Remote Login Servers in Linux

Let’s learn technology together

Understanding the Simple Use of Makefile and gdb!

Scan the QR code to follow my public account

Leave a Comment