Emrys Myrooin
Emrys Myrooin

Reputation: 2231

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.

Upvotes: 1

Views: 189

Answers (2)

downhillFromHere
downhillFromHere

Reputation: 1977

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.

Upvotes: 1

JesperE
JesperE

Reputation: 64444

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.

Upvotes: 1

Related Questions