#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2017
    Posts
    1
    Rep Power
    0

    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:

    Code:
    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:

    [output]
    In file ...
    ../B/B.h fatal error: A.h : no such file or directory
    #include "A.h"
    compilation terminated
    [/output]

    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.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,968
    Rep Power
    509
    I haven't tried to duplicate your configuration. I expect gcc to look in current directory so why would you even need ./ ?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo