简体   繁体   English

如何使用makefile调试项目

[英]How to debug project with makefile

I'm a newbie both in makefiles and in projects using multiple files. 我是makefile和使用多个文件的项目的新手。 What I'm working on now has one main.c and two user libraries. 我现在正在做的工作有一个main.c和两个用户库。 Here's the makefile: 这是makefile:

CC = gcc
OBJECTS = main.o path.o util.o

9_1 : $(OBJECTS)
    $(CC) -g $(OBJECTS) -o 9_1

main.o : main.c path.h util.h
    $(CC) -g -c main.c path.h util.h
path.o : path.c path.h util.h
    $(CC) -g -c path.c path.h util.h
util.o : util.c util.h
    $(CC) -g -c util.c util.h

.PHONY : clean
clean : 
    rm $(OBJECTS)

I'd like to be able to debug this project in Emacs (using the multiple-windows variable) however when I run gdb -i=mi 9_1 (the default option) and try to add breakpoints in main.c it doesn't let me. 我希望能够在Emacs中调试这个项目(使用multiple-windows变量)但是当我运行gdb -i=mi 9_1 (默认选项)并尝试在main.c中添加断点时它不会让我。 Specifically: I run b main.c:25 and I get 具体来说:我运行b main.c:25然后我就搞定了

No symbol table is loaded. Use the "file" command.
Breakpoint 1 (main.c:25) pending

What should I do? 我该怎么办?

It happened because 它发生的原因是

  • you already built your objects/executable without the -g flag 你已经构建了没有-g标志的对象/可执行文件
  • you added the -g flag afterwards 之后添加了-g标志

Adding an option in the makefile doesn't automatically trigger a recompilation. 在makefile中添加选项不会自动触发重新编译。 Since you have a clean target, just do 既然你有一个clean目标,那就做吧

make clean
make

and the objects are rebuilt with the -g option: executable is produced with symbols now. 并使用-g选项重建对象:现在可以使用符号生成可执行文件。 Happy debugging. 快乐的调试。

Aside: don't pass header files to the gcc command. 旁白:不要将头文件传递给gcc命令。 They're useful so make knows the dependencies (in the line above) but not when building the object from the .c file. 它们很有用所以make知道依赖关系(在上面的行中)但不是从.c文件构建对象时。 Doing so creates useless .o files. 这样做会创建无用的.o文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM