简体   繁体   中英

GDB doesn't see source files other than main, caused by makefile?

I have a semi-large project that I am trying to debug and for some reason gdb is only willing to show the source code of the main.c file and refuses to list any of the other files.

Here are the relevant lines in my makefile:

DEFINES= #...
BASE_CFLAGS= #...
BASE_LIBS= #...

#Load the required source files
HEADERS=$(wildcard *.h) $(wildcard subdir/*.h)  
SOURCES=$(HEADERS:.h=.c)
OBJECTS=$(SOURCES:.c=.o)

#Flags for each compilation type
CFLAGS=-Wall $(DEFINES)
main: CFLAGS+=$(BASE_CFLAGS) $(BASE_LIBS)
debug: CFLAGS+=$(BASE_CFLAGS) $(BASE_LIBS) -g -DDEBUG_MODE 

#Compilation rules for objects
%.o: %.c %.h
$(CC) -c $(CFLAGS) $< -o $@ 

#main compilation
main:$(OBJECTS) main.c
    $(CC) main.c $(OBJECTS) $(CFLAGS) -o ../main.out 

#debug compilation
debug:$(OBJECTS) main.c
    $(CC) main.c $(OBJECTS) $(CFLAGS) -o ../debug.out 

clean:
    rm $(OBJECTS)

For some reason when I run make debug then try to debug the resulting output it acts as though main.c is the only file compiled with the -g flag even though I ran make clean beforehand and inspected make's output to ensure that it did compile each object with the -g flag.

Before my most recent change I had a Makefile that looked more like this:

DEFINES= #...
BASE_CFLAGS= #...
BASE_LIBS= #...

#Load the required source files
HEADERS=$(wildcard *.h) $(wildcard subdir/*.h)  
SOURCES=$(HEADERS:.h=.c)
OBJECTS=$(SOURCES:.c=.o)

#main compilation
main:$(SOURCES) main.c
    $(CC) $(SOURCES) main.c -Wall $(DEFINES) $(BASE_CFLAGS) $(BASE_LIBS) -o ../main.out

#debug compilation
debug:$(SOURCES) main.c
    $(CC) $(SOURCES) main.c -Wall $(DEFINES) $(BASE_CFLAGS) $(BASE_LIBS) -g -DDEBUG_MODE -o ../debug.out

Which was obviously less efficient than my new solution but it did have a few advantages. Firstly I didn't have to run make clean between each main and debug compilation (I actually have a total of 8 different compilation rules, so having to clean between most of them to get the individual sources to be recompiled with the new options is a pain). Secondly and most importantly, GDB was able to see all of the source files when I ran the debug compilation. Now, as I said, it can only see main.c and lists the rest as "No source file named ???.c".

Therefore I have two questions:

  1. (not really important) Is there a way to compile each source independently, but force them to be recompiled with new options when a different compilation rule is selected in make.

  2. (very important, please help!) Why can't gdb see my other source files and what can I do to have it load them?

the following makefile can be invoked with make or with make -Ddebug depending on if you want to produce the main.out file or the debug.out file.

Note: the <tab> will need to be replaced with an actual tab char in your makefile

CC := /usr/lib/gcc
RM := /usr/lib/rm

ifndef debug
target := main.out
debugInfo :=
else
target := debug.out
debugInfo := -g -DDEBUG_MODE
endif

#Load the required source files
HEADERS=$(wildcard *.h) $(wildcard subdir/*.h)
SOURCES=$(HEADERS:.h=.c)
OBJECTS=$(SOURCES:.c=.o)

#Flags for each compilation type
CFLAGS+= $(debugInfo) -c -Wall -Wextra -Wconversion -std=gnu99

.PHONY : all
all : $(TARGET)

$(TARGET):$(OBJECTS)
<tab>$(CC) $(debugInfo) $^ -o $@ $(LFLAGS)

#Compilation rules for objects
%.o:%.c %.h
<tab>$(CC) $(CFLAGS) $< -o $@

.PHONY : clean
clean:
<tab>$(RM) $(OBJECTS)

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