Skip to content
Thomas Nilefalk edited this page Jul 13, 2020 · 2 revisions

Makefiles

Here's a macro that minimizes typing when defining yet another unittest with Cgreen and its runner:

# Macro for unittests that link the SUT and a small number of dependent .o
# $(1) = the SUT name and it assumes that the tests are in $(1)_tests.c
# $(2) = space separated list of extra .o to link
define UNITTEST
$(1)_DEPENDENCIES = $(2)
$(1)_DEPS = $$(patsubst %,$(OBJDIR)/%.o,$$($(1)_DEPENDENCIES))
$(1)_tests.so: $(OBJDIR)/$(1)_tests.o $(OBJDIR)/$(1).o $$($(1)_DEPS)
	gcc -shared -o $$@ $$^ $$(COVERAGE) $$(CGREEN)
endef

This allows you to do

$(eval $(call UNITTEST,options,protocol log position))
$(eval $(call UNITTEST,symboltable,hash))
$(eval $(call UNITTEST,symbol))

NOTE1: It requires GNU make since this is the only Make that supports macros with arguments.

NOTE2: the $(patsubst ... and $(OBJDIR) are there because I compile the objects to a subdirectory. Remove them if you don't.

Clone this wiki locally