Here is an example how to compile a list of .o files from their corresponding .c file using Static Pattern Rules:
CC = cc
SRCPATH = src
LIBPATH = lib
BINPG = pg
BINMY = my
SRCPG = $(BINPG).c
SRCMY = $(BINMY).c
LIBOBJS = usage.o commandline.o mode.o
LIBPGMY = libpgmy.a
# VPATH: Search Path for All Prerequisites
# http://www.gnu.org/software/make/manual/html_node/General-Search.html#General-Search
VPATH = src:lib
# "make OPT=0" from the command line skips the optimizer flag -O2, recommended when using gdb.
ifeq ($(strip$(OPT)),0)
CFLAGS =-g -ansi -pedantic -Wall
elseCFLAGS =-g -ansi -pedantic -Wall -O2
endifexportCFLAGSLDFLAGS = -L./$(LIBPATH)
LIBS = -lpgmy -mwindows
# Syntax of Static Pattern Rules
# http://www.gnu.org/software/make/manual/html_node/Static-Usage.html#Static-Usage
# See also:
# Pattern Rule Examples
# http://www.gnu.org/software/make/manual/html_node/Pattern-Examples.html#Pattern-Examples
.PHONY: all
all: lib $(SRCPG) $(SRCMY)
$(CC) $(LDFLAGS) $(SRCPATH)/$(SRCPG) -o $(BINPG) $(LIBS)$(CC) $(LDFLAGS) $(SRCPATH)/$(SRCMY) -o $(BINMY) $(LIBS).PHONY: lib
lib: $(LIBPATH)/$(LIBDB)
# $@ = the target (in lib/), $^ = the prerequisites (in src/)
$(LIBPATH)/$(LIBDB): $(LIBOBJS)
$(AR) $(ARFLAGS) $@ $(addprefix$(SRCPATH)/,$^)$(LIBOBJS): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $(SRCPATH)/$@.PHONY: clean
clean:
rm -rf $(BINPG) $(BINMY) $(BINPG).exe $(BINMY).exe $(SRCPATH)/*.o *core *~.PHONY: libclean
libclean:
rm -rf $(LIBPATH)/$(LIBDB)
This Makefile has been tested with a few simple C programs.
%%% TODO: mkdir pattern %%%
You can browse and download both the Makefile and the C sources to try it yourself.
Read more at Using Wildcard Characters in File Names from the GNU make manual.