title: How to compile files in a subdirectory with a makefile
body: <p>Here's what my directory looks like:</p>
  
  <pre><code>src/
      dir/
          file2.c
          file2.h
      file1.c
      file1.h
      makefile
  </code></pre>
  
  <p>When I use my makefile, I get:
  <code>gcc: error: file2.o: No such file or directory</code></p>
  
  <p>Using from src directory: <code>gcc -c dir/file2.c dir/file2.h</code>
  I get the correct .o which is created in <code>src</code>(that's what I want).
  But using the makefile, I get the error above.</p>
  
  <p>The simplest version of my makefile:</p>
  
  <pre><code>CFLAGS = -ansi -Wall -pedantic -g -I$(LIBRE_INC) -Iinclude -std=gnu99
  LDFLAGS = -lm
  EXEC = out
  CC = gcc
  
  out :  file1.o file2.o
      ${CC} -o $@ $^ $(CFLAGS) $(LDFLAGS)
  
  file1.o: file1.c file1.h
  
  file2.o: dir/file2.c dir/file2.h
  
  %.o : %.c
      ${CC} -c $&lt; $(CFLAGS) $(LDFLAGS)
  </code></pre>
  
  <p>The make gives:</p>
  
  <pre><code>gcc -c file1.c -ansi -Wall -pedantic -g -I -Iinclude -std=gnu99 -lm
  gcc -o out file1.o file2.o -ansi -Wall -pedantic -g -I -Iinclude -std=gnu99 -lm
  gcc: error: file2.o: No such file or directory
  makefile:7: recipe for target 'out' failed
  make: *** [out] Error 1
  </code></pre>
  
  <p>I know that the issue is coming from </p>
  
  <pre><code>%.o : %.c
      ${CC} -c $&lt; $(CFLAGS) $(LDFLAGS)
  </code></pre>
  
  <p>But I don't know how to replace it</p>
  
  <p>Edit:
  I added this line under every file where I had a subdir and it worked </p>
  
  <pre><code>${CC} -c $&lt; $(CFLAGS) $(LDFLAGS)
  </code></pre>
  
  <p>I'm still looking for better way if there are ! </p>