I'm starting with make, and I made my first makefile to build my project. I don't know if it's the best way to do it or not, so I share with you my makefile to get your opinion on it:
Directory structure:
├── LICENSE ├── Makefile ├── objs │ └── parser.o ├── out │ ├── libsip.a │ └── parser.h ├── README.md ├── src │ ├── parser.c │ ├── parser.d │ └── parser.h └── tests ├── a.out ├── config.ini ├── libsip.a ├── parser.h └── test.c
Makefile:
SHELL = /bin/sh
CC := gcc
CFLAGS := -Wall
SRCSDIR := src
OBJSDIR := objs
OUTDIR := out
SRCS := $(wildcard $(SRCSDIR)/*.c)
OBJS := $(SRCS:$(SRCSDIR)/%.c=$(OBJSDIR)/%.o)
$(OUTDIR)/libsip.a : $(OBJS)
@mkdir -p $(@D)
ar cr -o $@ $^
cp -f src/parser.h $(OUTDIR)
$(OBJSDIR)/%.o : $(SRCSDIR)/%.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $< -c -o $@
$(CC) -MM $< > $(patsubst %.c, %.d, $<) && \
sed -i \ '1s|^|$(@D)/|' $(patsubst %.c, %.d, $<)
# use sed to add path to object dependencies
.PHONY : clean
clean :
rm -rf $(OBJSDIR) $(OUTDIR)
-include $(SRCS:%.c=%.d)