简体   繁体   中英

Makefile doesn't rebuild objects on header modification

I have made a Makefile for compiling my C programm but it's not building object when i change one of the headers.

My MakeFile:

CC=gcc
CFLAGS=-O3 -Wall
LDFLAGS=-I/usr/include/mysql -L/usr/lib/x86_64-linux-gnu -lmysqlclient
SOURCES=$(wildcard *.c)
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=bin/beta_parser

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

%.o:%.c types.h cstes.h headers.h mysql.h 
    $(CC) -o $@ -c $< $(CFLAGS)

.PHONY: clean mrproper

clean:
    rm -rf *.o

mrproper:
    rm -rf $(EXEC)

What have I done wrong ?

EDIT : Corection of the Makeil after a great comment.

Scanning your sources for dependencies is outside the scope of Make (although there are other tools, such as CMake which will do this automatically). You need to add an explicit rule to generate these dependencies, but this can be done in many different ways. I've sometimes used the following technique:

OBJECTS = ....
-include $(OBJECTS:.o=.d)
...
$(OBJECTS): %.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@
    $(CC) $(CFLAGS) $(DEPFLAGS) $< > $*.d

Google for "make automatic dependency generation" will show you other ways to do it as well.

Although there are other more elegant tricks, in your case, I think something like

$(OBJECTS): types.h cstes.h headers.h mysql.h

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

should be sufficient.

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