makefile problem with 2 Dirs, 3 header Files

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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) $@ $^

run1:
	$(PROG1)



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
#include "A.h"
compilation terminated


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?

Thanks.
Last edited on
Registered users can post here. Sign in or register to post.