dylhunn
dylhunn

Reputation: 1434

Simplifying a makefile

I have written a scary-looking Makefile by copy/pasting suggestions from Stack Overflow. However, I have read that it might not be necessary to provide explicit compiler invocations so many times (for example, the -O3 flag is everywhere). How can I simplify this Makefile?

CFLAGS = -Weverything -Wno-padded -Wno-unused-parameter -Wno-unused-variable -Wno-sign-conversion
all: fianchetto.o util.o ttable.o movegen.o
    clang -O3 $(CFLAGS) -D NDEBUG $^ -o fianchetto
debugf: fianchetto.o ttable.o movegen.o
    clang -O3 $(CFLAGS) -g3 $^ -o fianchetto
clean:
    rm *.o && rm *.gch & rm fianchetto && rm -rf fianchetto.dSYM
%.o: %.c
    clang -O3 -c $(CFLAGS) $< -o $@
fianchetto.o: fianchetto.c
ttable.o: ttable.h ttable.c
movegen.o: movegen.h movegen.c
util.o: util.h util.c

I am mystified by a lot of the syntax, and would appreciate links or explanations of why simplifications work!

Upvotes: 2

Views: 260

Answers (1)

user657267
user657267

Reputation: 20990

  • CFLAGS and defines (which should be in CPPFLAGS anyway) are useless when linking
  • You're reinventing make's built-in rules, make will automatically link a target if one of its dependencies is "target.o" (in this case fianchetto: fianchetto.o). Make also knows how to compile C source files (as long as the source and object path match), so your pattern rule is superfluous too.
  • The object prerequisites aren't necessary as both clang and GCC can generate dependencies for you with the -M set of flags.
  • Compiling release and debug builds in the same dir makes for a more simple makefile, although you will need to remember to clean the object files when switching.
  • By default make assigns cc to CC, and cc should be a link to your system's default compiler, so you might not even need the first line below
CC       := clang
CPPFLAGS := -MMD -MP
CFLAGS   := -Weverything -Wno-padded -Wno-unused-parameter -Wno-unused-variable -Wno-sign-conversion -O3

objs := fianchetto.o util.o ttable.o movegen.o
deps := $(objs:.o=.d)

.PHONY: all debugf clean
all:    CPPFLAGS += -DNDEBUG
debugf: CFLAGS += -g3
all debugf: fianchetto 
fianchetto: $(objs)
clean: ; $(RM) $(objs) $(deps) fianchetto fianchetto.dSYM

-include $(deps)

Upvotes: 2

Related Questions