title: How to optimize this makefile?
body: <p>I'm trying to optimize this makefile for adding PGM.</p>
  <p>I created <code>makefile</code> needs to be optimized:</p>
  <pre><code>SOURCES = main.cpp logic.cpp
  OBJECTS = $(SOURCES:.cpp=.o)
  CC = CC
  MODEL001 = FLAGA FLAGB
  MODEL002 = FLAGC
  CFLAGS001 = -c $(MODEL001:%=-D%)
  CFLAGS002 = -c $(MODEL002:%=-D%)
  PGMNUM = 001 002
  
  all: PGM001 PGM002
  
  PGM001:$(SOURCES)
      $(CC) $(CFLAGS001) $(SOURCES)
      $(CC) -o $PGM001 $(OBJECTS)
  
  PGM002:$(SOURCES)
      $(CC) $(CFLAGS002) $(SOURCES)
      $(CC) -o $PGM002 $(OBJECTS)
  
  </code></pre>
  <p>Currently, I change the following variables when adding a PGM003.</p>
  <pre><code>ADD : MODEL003, CFLAGS003, PGM003:$(SOURCES)...
  MODIFY : PGMNUM, all
  </code></pre>
  <p>Just by adding <code>MODEL003</code> and <code>PGMNUM</code>, this makefile seems to work. How can I do it?</p>
  <p>(pseudo code like this but does not work):</p>
  <pre><code>SOURCES = main.cpp logic.cpp
  OBJECTS = $(SOURCES:.cpp=.o)
  CC = CC
  MODEL001 = FLAGA FLAGB
  MODEL002 = FLAGC
  MODEL003 = FLAGD FLAGE
  
  PGMNUM = 001 002 003
  
  CFLAGS$(PGMNUM) = -c $(MODEL$(PGMNUM):%=-D%)
  
  all: $(PGMNUM:%=PGM%)
  
  PGM$(PGMNUM):$(SOURCES)
      $(CC) $(CFLAGS$(PGMNUM)) $(SOURCES)
      $(CC) -o $PGM$(PGMNUM) $(OBJECTS)
  
  </code></pre>