Talk about how to use Gdb debugging tool (Makfile project management)

One, gdb debugging tool

gcc -g main.c -o main
commonly used commands:

  • Command shorthand function
  • help h List command classes by module
  • help class Find specific commands of a certain type
  • list l view code, can be followed by line number and function name
  • list-l View code online
  • quit q Quit gdb
  • run r run the program at full speed
  • start single step, run the program, stop at the first line to execute the statement
  • next n step by step execution
  • step s execute statement by statement, when a function is encountered, transfer to the function for execution
  • backtrace b View function call stack frame and hierarchical relationship
  • info I View the value of local variables inside the function
  • frame f switch function stack frame
  • finish ends the current function and returns to the point of function call
  • set set the value of the variable
  • run arvg[1] argv[2] command line passing parameters during debugging
  • print p print variables and addresses
  • break b set breakpoint, according to line number and function name
  • delete d delete breakpoints d breakpoints NUM
  • display set observation variables
  • undisplay Cancel the observation variable
  • continue to continue running the remaining code at full speed
  • enable breakpoints enable breakpoints
  • disable breakpoints disable breakpoints
  • x View memory x /20xw displays 20 units, hexadecimal, 4 bytes per unit
  • watch Print and display when the variable of the set observation point is sent and modified
  • i watch display observation point
  • core file ulimit -c 1024 open core file, gdb a.out core during debugging
  • set var Set the value of the variable in debugging, such as n=10 set var n=100

Two, gdb debugging mode

  • gdb debug mode
  • run at full speed
  • start single step debugging
  • set follow-fork-mode child/parent #Makefile project management tracking child process and parent process

Three, Makfile project management

  • Project code compilation management
  • Save compiling project time
  • Lifelong benefit once written

Operation example file: add.c sub.c mul.c dive.c main.c

Basic principles
Three elements: goals, conditions, and orders.

Fourth, the working principle of Makefile

  1. Analyze the relationship between each goal and dependency
  2. Execute commands from bottom to top based on dependencies
  3. Determine the update based on the modification time that is newer than the target

If the target does not depend on any conditions, execute the corresponding command to show the update.
clean

  • Purpose: Clear the intermediate o files and final target files produced by compilation
  • make clean If there is a clean file with the same name in the current directory, the command corresponding to clean will not be executed
  • False target statement: .PHONY:clean
  • Special symbols in the clean command

"-" This command is wrong, make will continue to execute subsequent commands. For example: "-rm main.o"
"@" does not display the command itself, only the result. Such as: "@echo"clean done""

other

-make默认执行第一个出现的目标,可通过make dst指定要执行的目标
-distclean目标
-install目标
-make -C指定目录 进入指定目录,调用里面的Makefile

Five, simply use MakeFile

python@ubuntu:~/linuxC/calc$ cat Makefile
target: dependent (condition) command
phase one

#app:add.c sub.c dive.c mul.c main.c
#gcc add.c sub.c dive.c mul.c main.c -o app

Stage two

#app:add.o sub.o dive.o mul.o main.o
#gcc add.o sub.o dive.o mul.o main.o -o app
#add.o:add.c
#gcc -c add.c
#sub.o:sub.c
#gcc -c sub.c
#dive.o:dive.c
#gcc -c dive.c
#mul.o:mul.c
#gcc -c mul.c
#main.o:main.c
#gcc -c main.c

Stage three

#$@表示目标,$^表示所有依赖,$<表示依赖中的第一个
#obj=add.o sub.o mul.o dive.o main.o
#src = $(wildcard *.c)
#obj = $(patsubst %.c,%.o,$(src))
#target = app
#$(target):$(obj)
#gcc $^ -o $@
#%.o:%.c
#gcc -c $< -o $@

Stage four

CPPFLAGS= -Iinclude
CFLAGS= -g -Wall
LDFLAGS=
CC=gcc
#CC=arm-linux-gcc
src = $(wildcard *.c)
obj = $(patsubst %.c,%.o,$(src))
target = app
$(target):$(obj)
$(CC) $^ $(LDFLAGS) -o $@
%.o:%.c
    $(CC) -c $< $(CFLAGS) $(CPPFLAGS) -o $@
.PHONY:clean
#彻底清除生生过程文件
clean:
    -rm -f *.o
    -rm -f app
#彻底清除生生过程文件和生成配置文件
distclean: rm /usr/bin/app
install:cp app /usr/bin
test:
    @echo $(src)
    @echo $(obj)

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/110232731