编辑 makefile 以允许在 linux 中进行调试
Editing a makefile to allow debugging in linux
我已经下载了一个基本的 makefile,我正在尝试编辑它以允许调试 linux 中的程序,但是即使在将 -g 标志添加到 makefile 之后,终端也只显示 [No源可用] 当 运行 gdbtui 命令而不是预期的调试信息时。
这是有问题的 makefile:
CC=gcc
EXE=myprog
CFLAGS=
LDFLAGS=
OPTCFLAGS=-O2
DEBUGCFLAGS=-g
OBJ=main.o util.o
# Default Target, dependency on $(EXE) target
all: $(EXE)
# Optimised target, add OPTCFLAGS
opt: CFLAGS+=$(OPTCFLAGS)
opt: $(EXE)
# Debug target, add DEBUG Flags
debug: CFLAGS+=$(DEBUGCFLAGS)
debug: $(EXE)
$(EXE): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $^
%.o:%.c
$(CC) $(CFLAGS) -c $<
.PHONY: clean test
clean:
rm *.o
rm $(EXE)
提前感谢大家的帮助。
您没有显示您正在使用的命令 运行 make。此 makefile 的默认目标 (all
) 根本不添加任何标志(这是一个奇怪的决定)。
为了构建优化版本,您必须 运行 make clean
后跟 make opt
。
要构建调试版本,您必须 运行 make clean
后跟 make debug
。
你到底做了什么?
您的 makefile 适用于 make debug
。
gdbtui
在您 运行 gdbtui myprog
之后报告 [ No Source Available ]
只是因为你还没有开始gdb
所以没有调试信息
尚未阅读 myprog
此时你会看到gdbtui
提示你:
---Type <return> to continue, or q <return> to quit---
输入<return>
;然后 gdb
仍然开始;源代码会出现,gdb
会等待
你的调试命令。
除了@MadScientist 注意到的关于默认目标的奇怪之处,
你的 makefile 中有这个小错误:clean
配方:
clean:
rm *.o
rm $(EXE)
应该是:
clean:
rm -f *.o
rm -f $(EXE)
或者简单地说:
clean:
rm -f *.o $(EXE)
没有-f
(force
)选项,如果你要删除目标文件
如果不删除 myprog
然后 运行 make clean
,它将在 rm *.o
处失败
而不是 运行 rm $(EXE)
.
我已经下载了一个基本的 makefile,我正在尝试编辑它以允许调试 linux 中的程序,但是即使在将 -g 标志添加到 makefile 之后,终端也只显示 [No源可用] 当 运行 gdbtui 命令而不是预期的调试信息时。
这是有问题的 makefile:
CC=gcc
EXE=myprog
CFLAGS=
LDFLAGS=
OPTCFLAGS=-O2
DEBUGCFLAGS=-g
OBJ=main.o util.o
# Default Target, dependency on $(EXE) target
all: $(EXE)
# Optimised target, add OPTCFLAGS
opt: CFLAGS+=$(OPTCFLAGS)
opt: $(EXE)
# Debug target, add DEBUG Flags
debug: CFLAGS+=$(DEBUGCFLAGS)
debug: $(EXE)
$(EXE): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $^
%.o:%.c
$(CC) $(CFLAGS) -c $<
.PHONY: clean test
clean:
rm *.o
rm $(EXE)
提前感谢大家的帮助。
您没有显示您正在使用的命令 运行 make。此 makefile 的默认目标 (all
) 根本不添加任何标志(这是一个奇怪的决定)。
为了构建优化版本,您必须 运行 make clean
后跟 make opt
。
要构建调试版本,您必须 运行 make clean
后跟 make debug
。
你到底做了什么?
您的 makefile 适用于 make debug
。
gdbtui
在您 运行 gdbtui myprog
之后报告 [ No Source Available ]
只是因为你还没有开始gdb
所以没有调试信息
尚未阅读 myprog
此时你会看到gdbtui
提示你:
---Type <return> to continue, or q <return> to quit---
输入<return>
;然后 gdb
仍然开始;源代码会出现,gdb
会等待
你的调试命令。
除了@MadScientist 注意到的关于默认目标的奇怪之处,
你的 makefile 中有这个小错误:clean
配方:
clean:
rm *.o
rm $(EXE)
应该是:
clean:
rm -f *.o
rm -f $(EXE)
或者简单地说:
clean:
rm -f *.o $(EXE)
没有-f
(force
)选项,如果你要删除目标文件
如果不删除 myprog
然后 运行 make clean
,它将在 rm *.o
处失败
而不是 运行 rm $(EXE)
.