3

i love this makefile but if i call 'all' or 'lab4' it will -o the name 'lab4' but build using the dbug receipe. if i call 'dbug' it will -o 'dbug' and use dbug receipe. what am i doing wrong? i stole this from wiki.osdev.org/Makefile btw

CC := gcc
CFLAGS := -c -Wall -Werror
DBCFLAGS := $(CFLAGS) -DDEBUG -g
LDFLAGS :=
LAB := lab4

PROJDIRS := .
SRCFILES := $(shell find $(PROJDIRS) -maxdepth 1 -type f -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -maxdepth 1 -type f -name "*.h")
OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))
TSTFILES := $(patsubst %.c,%_t,$(SRCFILES))
AUXFILES := makefile README
ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)

-include $(DEPFILES) $(TSTDEPFILES)

.PHONY: all clean dist check testdrivers todolist

all: $(LAB)

$(LAB): $(OBJFILES)
    $(CC) $(LDFLAGS) $(OBJFILES) -o $@

%.o: %.c makefile
    $(CC) $(CFLAGS) $< -o $@

dbug: $(OBJFILES)
    $(CC) $(LDFLAGS) $(OBJFILES) -o $@

%.o: %.c makefile
    $(CC) $(DBCFLAGS) $< -o $@

todolist:
    -@for file in $(ALLFILES:makefile=); do grep -H -e TODO -e FIXME $$file;     done; true

dist:
    @tar czf /mnt/backup/cis27/$(LAB)-`date +%m%d%y.%H%M`.tar.gz $(ALLFILES)

clean:
    -rm *.o *~ *.d 
  • "it will -o the name 'lab4' but build using the dbug receipe" Don't think so. It's using the (identical) $(LAB) recipe. – goldilocks Mar 8 '14 at 19:13
1

I changed the makefile a little. The main difference is

dbug: 
    @$(MAKE) CFLAGS="$(DBCFLAGS)" LDFLAGS="$(DBLDFLAGS)"

So if you run make dbug CFLAGS and LDFLAGS are redefined again.

CC := gcc
CFLAGS = -O2 -c -Wall -Werror
DBCFLAGS = $(CFLAGS) -O0 -DDEBUG -g
LDFLAGS =
DBLDFLAGS = $(LDFLAGS) -g
LAB := lab4

PROJDIRS := .
SRCFILES := $(shell find $(PROJDIRS) -maxdepth 1 -type f -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -maxdepth 1 -type f -name "*.h")
OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))
TSTFILES := $(patsubst %.c,%_t,$(SRCFILES))
AUXFILES := makefile README
ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)

-include $(DEPFILES) $(TSTDEPFILES)

.PHONY: all clean dist check testdrivers todolist

all: $(LAB)

dbug: 
        @$(MAKE) CFLAGS="$(DBCFLAGS)" LDFLAGS="$(DBLDFLAGS)"

$(LAB): $(OBJFILES)
        $(CC) $(LDFLAGS) $(OBJFILES) -o $@

%.o: %.c makefile
        $(CC) $(CFLAGS) $< -o $@

todolist:
        -@for file in $(ALLFILES:makefile=); do grep -H -e TODO -e FIXME $$file;     done; true

dist:
        @tar czf /mnt/backup/cis27/$(LAB)-`date +%m%d%y.%H%M`.tar.gz $(ALLFILES)

clean:
        -rm *.o *~ *.d 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.