title: Using vpath and wild card in Makefile
body: <p>1) I am having directory called dir, and the subdirectories are sub1, sub2, sub3.</p>
  
  <p>2) I want to create a Makefile which will compile all the sources in the subdirectory. These are some lines of my makefile.</p>
  
  <pre><code>CFLAGS = -I/usr/include/ -I./ -ansi -g
  GPROF_CFLAGS = -I/usr/include/ -I./
  VPATH = ./sub1 ./sub2 ./sub3
  SRCS := $(wildcard *.c)
  OBJS=$(SRCS:.c=.o)
  exe:    $(OBJS)
          $(CC) $(LFLAG) -o exe $(CFLAGS) $(OBJS) -lm 
  
  exe_gprof:
          gcc $(LFLAG) -o exe $(GPROF_SUFFIX) $(GPROF_CFLAGS) $(SRCS)
  
  clean:
          rm -f *.o
  
  clean_all: clean
          rm -f exe exe$(GPROF_SUFFIX) gmon.out Simfir000.stat rm -f *.o.
  </code></pre>
  
  <p>3) This make file works fine, if i have only sub folder called sub(this directory contains all source files) and no other sub folder. (i.e) in the make file vpath=./sub.</p>
  
  <p>4) If there are sub folders as I mentioned in point 1, the main file is in the subfolder of sub1/src/main.c(i.e sub folder inside the sub folder). If I try to compile. It's giving undefined ref to main.</p>
  
  <p>5) what change I have to do, to compile this successfully. Can we give more than one directory path in vpath?.</p>
  
  <p>Overall map is:</p>
  
  <blockquote>
    <p>Having directory called dir</p>
    
    <p>sub directory dir/sub1</p>
    
    <p>dir/sub2</p>
    
    <p>dir/sub3</p>
    
    <p>main file is in dir/sub1/src/main.c</p>
  </blockquote>
  
  <p>My second question is<br>
  If I want to execute directory by directory and create different exe's</p>
  
  <blockquote>
    <p>compiling sub1 only and create sub1exe</p>
    
    <p>copiling sub2 only and create sub2exe.</p>
  </blockquote>
  
  <p>The make file will be </p>
  
  <pre><code>CFLAGS = -I/usr/include/ -I./ -ansi -g
  GPROF_CFLAGS = -I/usr/include/ -I./
  VPATH = ./sub1 ./sub2 ./sub3
  SRCS1 := $(wildcard *.c)
  SRCS2 := $(wildcard *.c)
  OBJS=$(SRCS:.c=.o)
  exe1:    $(OBJS)
          $(CC) $(LFLAG) -o exe1 $(CFLAGS) $(OBJS) -lm 
  exe2:    $(OBJS)
          $(CC) $(LFLAG) -o exe2 $(CFLAGS) $(OBJS) -lm 
  
  exe1_gprof:
          gcc $(LFLAG) -o exe1 $(GPROF_SUFFIX) $(GPROF_CFLAGS) $(SRCS1)
  exe2_gprof:
          gcc $(LFLAG) -o exe2 $(GPROF_SUFFIX) $(GPROF_CFLAGS) $(SRCS2)
  
  clean:
          rm -f *.o
  
  clean_all: clean
  </code></pre>
  
  <p>What changes I have to make instead of $(wildcard *.c).</p>