我正在尝试编译我的项目以使用 Makefile 进行调试。 makefile 当前生成多个不同的目标文件,然后从所有这些不同的目标文件编译单个可执行文件。 make文件如下:
CC=gcc
Scheduler: scheduler-exec.o scheduler-impl.o lab5_linkedlist.o lab5_queue.o
$(CC) -o Scheduler scheduler-impl.o scheduler-exec.o lab5_linkedlist.o lab5_queue.o
scheduler-exec.o: scheduler-exec.c
$(CC) -c -o scheduler-exec.o scheduler-exec.c
scheduler-impl.o: scheduler-impl.c
$(CC) -c -o scheduler-impl.o scheduler-impl.c
queue.o: lab5_queue.c
$(CC) -c -o lab5_queue.o lab5_queue.c
linkedlist.o: lab5_linkedlist.c
$(CC) -c -o lab5_linkedlist.o lab5_linkedlist.c
clean:
rm *.o
我想维护这个分布式编译系统,以便生成这些不同的目标文件,然后使用 Makefile 中的 Scheduler
任务将它们编译在一起。我尝试将 -g
标志添加到所有任务,但是当我在生成的 Scheduler
可执行文件上运行 gdb
时,它仍然没有有调试信息。 gdb
程序的输出是这样的:
GNU gdb (GDB) 8.3
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin18.5.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from Scheduler...
--Type <RET> for more, q to quit, c to continue without paging--
程序就卡在那里了。我希望有一种方法可以使用 Makefile 编译该程序并能够获取变量信息。谢谢
我尝试了以下 makefile,但它不起作用:
CC=gcc
Scheduler: scheduler-exec.o scheduler-impl.o lab5_linkedlist.o lab5_queue.o
$(CC) -o Scheduler scheduler-impl.o scheduler-exec.o lab5_linkedlist.o lab5_queue.o -g
scheduler-exec.o: scheduler-exec.c
$(CC) -c -o scheduler-exec.o scheduler-exec.c -g
scheduler-impl.o: scheduler-impl.c
$(CC) -c -o scheduler-impl.o scheduler-impl.c -g
queue.o: lab5_queue.c
$(CC) -c -o lab5_queue.o lab5_queue.c -g
linkedlist.o: lab5_linkedlist.c
$(CC) -c -o lab5_linkedlist.o lab5_linkedlist.c -g
clean:
rm *.o
最佳答案
删除大部分规则,而是使用内置规则:
CC := gcc
CFLAGS += -g
CFLAGS += -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds
Scheduler: scheduler-exec.o scheduler-impl.o lab5_linkedlist.o lab5_queue.o
$(LINK.c) $^ $(LDLIBS) -o $@
clean:
rm *.o
这就是您所需要的。现在重建一切(make -B
)。您可以检查是否有调试信息:
$ file Scheduler
输出会是这样的
Scheduler: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=5812e87410be09796cfed309a265dbcff1cf5d5e, for GNU/Linux 3.2.0, with debug_info, not stripped
关于c - 使用makefile编译时如何设置GDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430524/