4.4 But mine are C++ programs

Fine. You can enhance the Makefile to work with C++ programs, as well.

CC = g++ # change to gcc for C programs
CXXFLAGS = -O -Wall -g
LDFLAGS = -g
CSRC = main.c array.c
CCSRC = ui.cc
EXE = main
  
$(EXE):  $(CSRC:.c=.o) $(CCSRC:.cc=.o)

#array.o:  array.c array.h
#	gcc $(CXXFLAGS) -c array.c

#main.o:  main.c array.h
#	gcc $(CXXFLAGS) -c main.c
  
%.d: %.c
	 set -e; gcc $(CXXFLAGS) $^ -MM \
	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ] || rm -f $@ 
  
%.d: %.cc
	 set -e; gcc $(CXXFLAGS) $^ -MM \
	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ] || rm -f $@ 
 
clean:
	rm -f $(EXE) $(CSRC:.c=.o) $(CSRC:.c=.d)  $(CCSRC:.cc=.o) $(CCSRC:.cc=.d)

include $(CSRC:.c=.d)
include $(CCSRC:.cc=.d)

Here, we separate our source files into two groups. CSRC keeps track of a list of C source files, whereas CCSRC keeps track of a list of C++ source files. We need to add CCSRC to the prerequisite of main. We also have to add a new rule to generate .d files from .cc files. Note that the command to generate a dependency specification from a C++ file is the same as that of a C file.



Copyright © 2006-10-08 by Tak Auyeung