Skip to content

Instantly share code, notes, and snippets.

@itiut
Last active August 17, 2017 03:47
Show Gist options
  • Save itiut/102bbe0db7dea0472fafc468990003da to your computer and use it in GitHub Desktop.
Save itiut/102bbe0db7dea0472fafc468990003da to your computer and use it in GitHub Desktop.
A simple Makefile for unit testing with Criterion
## Constants
CC := gcc
CFLAGS := -Wall -O2 -MMD -MP
## Variables
program := main
test-program := test-main
# Follow the file name convention like Go:
# a unit test file `foobar_test.c` for a source file `foobar.c`
test-sources := $(wildcard *_test.c)
test-objects := $(test-sources:.c=.o)
test-target-sources := $(test-sources:_test.c=.c)
test-target-objects := $(test-target-sources:.c=.o)
sources := $(filter-out $(test-sources), $(wildcard *.c))
objects := $(sources:.c=.o)
deps := $(test-objects:.o=.d) $(objects:.o=.d)
## Dependencies
$(program): $(objects)
$(test-program): LDLIBS += -lcriterion
$(test-program): $(test-objects) $(test-target-objects)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
-include $(deps)
## Task targets
.DEFAULT_GOAL := all
.PHONY: all
all: $(program)
.PHONY: clean
clean:
-$(RM) $(program) $(test-program) *.o *.d
.PHONY: test
test: $(test-program) run-test
# private
.PHONY: run-test
run-test:
./$(test-program) --verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment