c - 使用makefile编译时如何设置GDB?

标签 c macos makefile gdb

我正在尝试编译我的项目以使用 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/

相关文章:

macos - 当我在具有多个显示器的 macOS 中切换桌面时,为什么焦点会切换到另一个意外的应用程序(在我的情况下是 iTerm2 或 VSCode)?

C++ Makefile %.c 有效,%.cpp 无效?

c++ - 是否可以将地址映射到函数的结果?

c - 在字符后读取多个数字

将数据复制/扫描/读取到未初始化的指针时崩溃或 "segmentation fault"

c++ - 在 GCC 上构建的函数多重定义链接器错误 - Makefile 问题

c - 如何使一个 makefile 引用另一个 makefile

c - 我可以将什么输入用于系统函数 time() 以使其返回 -1(错误条件)?

macos - 生成自签名 SSL 证书,权限错误 (OSX)

macos - 在 Mac 上使用 Python 打印到 USB 打印机