title: make for compiling — all *.c files in folders & subfolders in project
body: <p>To compile two files i have created a makefile where i use to mention the object name or i can use the pattern rule using patsubst.</p>
  
  <pre><code># ----------------------------------------------------------------------------
  # Makefile for building tapp
  #
  # Copyright 2010 FriendlyARM (http://www.arm9.net/)
  #
  
  ifndef DESTDIR
  DESTDIR            ?= /opt/FriendlyARM/tiny6410/linux/rootfs_qtopia_qt4
  endif
  
  #CFLAGS              = -c -Wall -O2  # wall is for warning show and 02 is optiminisation level 2
  CFLAGS              = -c -O2  # wall is for warning show and 02 is optiminisation level 2
  #CC                    = arm-linux-gcc   # compiler name
  CC                    = gcc   # compiler name
  LD                    = ld
  
  INSTALL             = install         # 
  
  TARGET              = led_player_project
  
  #OBJ = led-player_backup.o led-player.o
  OBJ := $(patsubst %.c,%.o,$(wildcard *.c */*.c))
  
  #OBJ = $(shell find . -name '*.c')
  
  all: $(TARGET) 
  #all: $(OBJ)
  
  led_player_project : $(OBJ)
      $(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
  #       $(LD) $(LDFLAGS) -o $@ $&lt; $(LIBS)
  
  %.o : %.c
      $(CC) $(CFLAGS) $&lt; -o $@
  
  #$&lt; -o $@
  
  
  install: $(TARGET)
      $(INSTALL) $^ $(DESTDIR)/usr/bin
  
  clean :
      rm -rf *.o $(TARGET) $(OBJ)
  
  
  # ----------------------------------------------------------------------------
  
  .PHONY: $(PHONY) install clean
  
  # End of file
  # vim: syntax=make
  
  #EOF
  </code></pre>
  
  <p>Now if my project contains folder contains subfolders &amp; they contains further files. Then can i write pattern rule to compile every file &amp; create an common executable?  </p>
  
  <p>1> Do i will have to create makefile in every-subfolder so that i can invoke that makefile from main makefile, like integrating static driver to linux kernel each driver have respective makefile ?<br>
  2> Or common makefile for full project ?<br>
  3> can i use patsubst to compile every file without mentioning there name.<br>
  4> How can i combine every *.o to create on executable called <code>main</code>.</p>
  
  <p><img src="https://i.stack.imgur.com/me5z6.png" alt="enter image description here"></p>
  
  <p>Edit :---<br>
  @Jan Hudec
  I have modified my makefile as per your comment (i have posted it above). Now i am just trying with two folders inside my main folder. I am getting following error
  Folder structure :--</p>
  
  <pre><code>main Folder  ----&gt; one Folder 
               ----&gt; two Folder 
  </code></pre>
  
  <p>Folder Main contains :--  </p>
  
  <pre><code>main.c 
  main.h
  Makefile
  </code></pre>
  
  <p>Folder one contains :--</p>
  
  <pre><code>one.c
  one.h
  </code></pre>
  
  <p>Folder two contains :--</p>
  
  <pre><code>two.c
  two.h
  </code></pre>
  
  <p>main.c content :--</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "main.h"
  
  int main()
  {
    char *p;
  
    printf("\n\n main \n");
  
    one();
    two();
  
    return 0;
  }
  </code></pre>
  
  <p>main.h content :---</p>
  
  <pre><code>#include "one/one.h"
  #include "two/two.h"
  </code></pre>
  
  <p>one.c content :---</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
  
  #include "one.h"
  
      void one()
      {
  
        printf("\n one \n");
  
      }
  </code></pre>
  
  <p>one.h content :---</p>
  
  <pre><code>void one();
  </code></pre>
  
  <p>two.c content :---</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "two.h"
  
  void two()
  {
  
    printf("\n two \n");
  
  }
  </code></pre>
  
  <p>two.h content :---</p>
  
  <pre><code>void two();
  </code></pre>
  
  <p>Error i got at make time :----</p>
  
  <pre><code>ignite@ignite:~/testing/main$ make
  gcc    -c -O2   main.c -o main.o
  gcc    -c -O2   one/one.c -o one/one.o
  gcc    -c -O2   two/two.c -o two/two.o
  ld  -o led_player_project main.o one/one.o two/two.o 
  ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080
  main.o: In function `main':
  main.c:(.text.startup+0x11): undefined reference to `puts'
  one/one.o: In function `one':
  one.c:(.text+0xb): undefined reference to `puts'
  two/two.o: In function `two':
  two.c:(.text+0xb): undefined reference to `puts'
  make: *** [led_player_project] Error 1
  ignite@ignite:~/testing/main$ 
  </code></pre>