Hi all,
I'm learning how to use makefile to build an executable file in Windows 7. The compiler I used is mingw32. I followed the examples 18-1 to 18-3 from the book <Practical C Programming>. Three files were used here: ia.c ia.h, and hist.h. This book provides some makefile examples and they work fine as long as I put all three files and makefile under one folder.
However, I wanted to see if I could make it when putting source files and header files into seperate folders. So I followed the traditional layout mentioned in the GNU make manual:
Code:
~/hist
|>---- Makefile
|>---- include
| |>---- ia.h
|>---- src
| |>---- hist.c
| |>---- ia.c
Now, I should use VPATH/vpath or -I../path. So I tried different combinations, but all of them failed. I really don't know why. Here's my makefile:
Code:
VPATH = C:\hist\include
Srcdir = -I C:\hist\src
CC = gcc
CFLAGS = -g -Wall
all: hist
hist: hist.o ia.o
$(CC) $(CFLAGS) -o hist hist.o ia.o
hist.o: hist.c ia.h
$(CC) $(Srcdir) $(CFLAGS) -c hist.c
ia.o: ia.c ia.h
$(CC) $(Srcdir) $(CFLAGS) -c ia.c
clean:
rm -f hist hist.o ia.o
-----------------------------------
The mingw gives me this error:
-----------------------
$ make
make: *** No rule to make target `hist.c', needed by `hsit.o'. Stop.
----------------------------------
I also tried the following possibility:
-----------------------------------
IDIR = -I C:\hist\include
SDIR = -I C:\hist\src
CC = gcc
CFLAGS = -g -Wall
all: hist
hist: hist.o ia.o
$(CC) $(CFLAGS) -o hist hist.o ia.o
hist.o: hist.c ia.h
$(CC) $(CFLAGS) -c c:\hist\src\hist.c ia.h
ia.o: ia.c ia.h
$(CC) $(CFLAGS) -c ia.c ia.h
clean:
rm -f hist hist.o ia.o
I got the same error. Then I tested the vpath method:
Code:
vpath %.c src
vpath %.h include
CC = gcc
CFLAGS = -g
srcf := hist.c ia.c
obj := hist.o ia.o
%: %.c
$(obj) $^ -o $@
%.o: %.c
$(srcf) $(obj) $<
clean:
rm -f ia
Now I got no error, and the Mingw gives me this output:
Code:
$ make
rm -f ia
# gcc -g -o hist hist.o ia.o
# gcc -g -c c:\hist\src\hist.c ia.h
# gcc -g -c ia.c ia.h
# rm -f hist hist.o ia.o
# gcc -g -o hist hist.o ia.o
# gcc -g -c hist.c
# gcc -g -c ia.c
# rm -f hist hist.o ia.o
Regarding the vpath method, I tried the following ways too:
Code:
vpath %.c c:\hist\src
vpath %.h c:\hist\include
----------------------------
vpath %.c c:\\hist\\src
vpath %.h c:\\hist\\include
-------------------------------
vpath %.c c:/hist/src
vpath %.h c:/hist/include
Mingw gave me same output as above.
So what should I do to add the path? Any form of help would be appreciated! Thanks for reading.