title: How do I make Makefile to recompile only changed files?
body: <p>I have been struggling a bit to get make to compile only the files that have been edited. However I didn't have much success and all the files get recompiled. Can someone explain me why?</p>
  
  <p>My files are:</p>
  
  <pre><code>main.c
  a_functions.c
  </code></pre>
  
  <p>where <strong>main.c</strong> includes <em>main.h</em> 
  and <strong>a_functions.c</strong> includes <em>a.h</em></p>
  
  <p>Here is my makefile:</p>
  
  <pre><code>CC=gcc
  CFLAGS=-Wall -I. -c
  EXEC_FILE=program1
  
  
  all: program
  
  a_functions.o: a_functions.c
  a_functions.c: a.h
  main.o: main.c
  main.c: main.h
  
  objects: a_functions.c main.c
      $(CC) a_functions.c main.c $(CFLAGS)
  
  program: a_functions.o main.o
      $(CC) a_functions.o main.o -o $(EXEC_FILE)
  </code></pre>
  
  <p>Changing the makefile as per suggestions seems to have the same problem::</p>
  
  <pre><code>all: program
  
  a_functions.o: a_functions.c a.h
      gcc a_functions.c -c
  
  main.o: main.c main.h
      gcc main.c -c
  
  program: a_functions.o main.o
      gcc a_functions.o main.o -o program1
  </code></pre>