I have 2 Dirs and 3 header files stored in them, as follows:
Dir A : contains Header files A and C
Dir B : contains Header file B
Dirs A and B are sub-directories of a dir named Apps (which is a subdir of some other dirs).
Header file B #includes header file A.
Header file C #includes header file B.
I have tester source files (.cpp) for header files A, B and C, named TestA, TestB and TestC respectively, in the same dirs as their corresponding header files.
The make file for TestC is shown below:
CPP = g++
OFLAG = -o
CFLAG = -std=c++11 -c
PROG1 = TestC
HC = C
HB = B
HA = A
HBDIR = ../B/
HADIR = ./
IHBDIR = -I$(HBDIR)
IHADIR = -I$(HADIR)
all : $(PROG1).o $(PROG1).exe run1
$(PROG1).o : $(PROG1).cpp $(HADIR)$(HC).h $(HBDIR)$(HB).h $(HADIR)$(HA).h
$(CPP) $(CFLAG) $(IHADIR) $(IHBDIR) $<
$(PROG1).exe : $(PROG1).o
$(CPP) $(OFLAG) $@ $^
This make file is unable to locate header file A (which is #included by header file B) and gives the following error:
In file ...
../B/B.h fatal error: A.h : no such file or directory
Evidently, make is searching for A.h in B.h's dir (since B.h #includes A.h). However, I have defined the symbol HADIR (as ./), which should have caused make to search in the default dir, which is Dir A.
It works properly if I define HADIR as ../A. But I don't understand why I should do that.
Why does this failure occur?