Skip to content

Instantly share code, notes, and snippets.

@maxbeutel maxbeutel/Makefile
Last active May 5, 2016

What would you like to do?
somewhat clean C makefile with dependencies
### mostly stolen from
CC = clang
SHELL = /bin/bash
CFLAGS += -std=c11 -pedantic -Werror -Wall -Wextra -Isrc
DEV_CFLAGS = -O0 -ggdb
SOURCES=$(wildcard src/**/*.c src/*.c)
LIBOBJ=$(patsubst %.c,%.o,$(SOURCES))
TEST_SOURCES=$(wildcard tests/**/*.c tests/*.c)
TESTS=$(patsubst %.c,%,$(TEST_SOURCES))
# name of output library
all: build build/lib$(LIB).a build/lib$(LIB).so check
all-dev: CFLAGS += $(DEV_CFLAGS)
all-dev: all
%.o: %.c .deps
$(CC) $(CFLAGS) -c -o $@ $< -MMD -MP -MF .deps/$*.d
# binaries (test files)
%: %.c .deps build/lib$(LIB).so
$(CC) $(CFLAGS) -o $@ $< build/lib$(LIB).so -MMD -MP -MF .deps/$*.d
build/lib$(LIB).a: $(LIBOBJ)
$(AR) -rcs $@ $(LIBOBJ)
ranlib $@
build/lib$(LIB).so: $(LIBOBJ)
$(CC) -shared -o $@ $(LIBOBJ)
check: $(TESTS)
for name in $(TESTS); do echo ./$$name; ./$$name; done
check-dev: CFLAGS += -O0 -ggdb
check-dev: check
mkdir -p build
rm -rf .deps
rm -rf build
find ./tests -name "*_test" -delete
find . \( -name "*.o" -o -name "*.a" -o -name "*.dSYM" \) -delete
mkdir -p .deps/src
mkdir -p .deps/tests
# # subtle: directories are listed as changed when entries are
# # created, leading to spurious rebuilds.
# .deps/stamp:
# mkdir -p .deps/src && touch/stamp
-include .deps/src/*.d
-include .deps/tests/*.d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.