# Makefile 4: Using implicit rules ################################### # Overriding default macros # For compiling C files CC = gcc CFLAGS = -Wall -g # For compiling C++ files CXX = g++ CXXFLAGS = -Wall -g # For linking LDFLAGS = # For creating a library AR = ar ARFLAGS = rc # End of defaults ################################### ################################### # Let's define sone additional variables RANLIB = ranlib PROGRAMS = main-stack main-queue MAIN_STACK_DEPS = main-stack.o stack.o LIB_DEPS = queue.o stack.o LIB = libdata.a MAIN_QUEUE_DEPS = main-queue.o $(LIB) MAIN_QUEUE_LIBS = -L. -ldata ################################### all: $(PROGRAMS) # Using pattern rules # The order of the following two pattern rules is important # First pattern rules main-%.o: main-%.c %.h $(CC) $(CFLAGS) -c $< # Second pattern rule %.o: %.c %.h $(CC) $(CFLAGS) -c $< libdata.a: $(LIB_DEPS) $(AR) $(ARFLAGS) $@ $(LIB_DEPS) $(RANLIB) $@ # Thanks to implicit rules, we can often avoid # writing any rule at all or we can specify the # dependencies only and let make figure out what to do with them main-stack: $(MAIN_STACK_DEPS) main-queue: $(MAIN_QUEUE_DEPS) $(CC) $(LDFLAGS) -o $@ $< $(MAIN_QUEUE_LIBS) clean: rm -f *.o $(PROGRAMS) $(LIB)