Here is an example how to group targets by filename, using wildcards:
# Makefile using wildcards in targets
CC = gcc
CFLAGS = -g -ansi -pedantic -Wall -O2
# Get all C source files in this directory with prefix pthread_*.
PTHREAD_TARGETS = $(patsubst%.c,%,$(wildcardpthread_*.c))
# The patsubst() function strips the .c extension from the filename.
# To compile for example 'pthread_hello.c', type
# 'make pthread_hello' (without .c extension)
# Applying the same method for prefix math_*
MATH_TARGETS = $(patsubst%.c,%,$(wildcardmath_*.c))
# 'pthread targets', add the -lpthread flag
.PHONY : $(PTHREAD_TARGETS)
$(PTHREAD_TARGETS):
@echo This target will be compiled with the -lpthread flag.$(CC) $(CFLAGS) $@.c -o $@ -lpthread# 'math targets', add the -lm flag.
.PHONY : $(MATH_TARGETS)
$(MATH_TARGETS):
@echo This target will be compiled with the -lm flag.$(CC) $(CFLAGS) $@.c -o $@ -lm# Targets not matching any of the rules above will be compiled using the following implicit rule:
# $(CC) $(CFLAGS) $@.c -o $@# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
.PHONY : clean
clean:
rm -rf *.o *.exe *~ *.core core