简体   繁体   中英

How to setup GDB when compiling using makefile?

I am trying to compile my project for debugging using a Makefile. The makefile currently generates multiple different object file and then compiles a single executable from all of these different object file. The make file is as follows:

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 

I would like to maintain this distributed compiling system such that I am generating these different object files and then compiling them together using the Scheduler task in the Makefile. I tried adding the -g flag to all the tasks but when i ran gdb on the Scheduler executable that was generated, it still didn't have the debugging information. The output of the gdb program is this:

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--

and the program is just stuck there. I would appreciate a method where I can compile this program using the Makefile and be able to get the variable information. Thanks

I tried the following makefile it didnt work:

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 

Remove most of your rules, and instead use the built-in rules:

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

That's all you need. Now rebuild everything ( make -B ). You can check that you have debugging information:

$ file Scheduler

Output will be something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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