Reputation: 153
Let' say I have library project with a few folders:
The problem is, everytime I change a header file and make doesn't perceive this (which is obvious why if you look at my Makefile) and says everything is up to date. I have manage to solve this with another project which wasn't a library, but with this one everytime I try something I end up with an error when the target is getting built. Here is the Makefile:
CC=gcc
CFLAGS=-g -O2 -Wall -Iinclude -rdynamic -DNDEBUG $(OPTFLAGS)
LDFLAGS=$(OPTLIBS)
SOURCES=$(wildcard src/*.c)
OBJECTS=$(patsubst src/%.c,build/%.o,$(SOURCES))
TARGET=lib/libdatastruct.a
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
# The Target lib
all: $(TARGET) $(SO_TARGET)
dev: CFLAGS=-g -Wall -Iinclude -Wall -Wextra $(OPTFLAGS)
dev: all
$(TARGET): CFLAGS += -fPIC
$(TARGET): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS)
build/%.o: src/%.c
$(CC) $(CFLAGS) -o $@ -c $<
build:
@mkdir -p lib
@mkdir -p build
clean:
rm -rf lib build
Upvotes: 0
Views: 2321
Reputation: 153
I was able to solve this myself. Changed this:
build/%.o: src/%.c
$(CC) $(CFLAGS) -o $@ -c $<
To this:
build/%.o: src/%.c
$(CC) $(CFLAGS) -MMD -o $@ -c $<
include $(DEPS)
$(DEPS): ;
And added:
DEPS=$(patsubst %.o,%.d,$(OBJECTS))
After:
OBJECTS=$(patsubst src/%.c,build/%.o,$(SOURCES))
Upvotes: 1
Reputation: 25753
Generate a list of headers:
HEADERS := $(wildcard src/*.h)
and since you don't have any dependency files, simply ensure that all object files depend on all header files:
$(OBJECTS): $(HEADERS)
If any header is modified every object file is rebuild since any source file may include (and depend) on any header file.
If you don't want to rebuild everything after a header is changed, you can manually add specific dependencies, so that only the needed files are rebuilt. For example:
src/file.c: src/file.h
src/main.c: src/main.h src/file.h
Those dependencies can also be made automatically.
Upvotes: 0