235711131719
235711131719

Reputation: 37

Telling makefile to search different directory for C header file(s)

I've been trying to figure this out for a little while now and I am at the point where I am out of guesses/ideas.

I am making some C programs for a numerical analysis class at school and many of the programs use utility functions that I've written beforehand. I'd like to have the utility "library" in a separate directory from the class programs, but be able to include the utility header files when needed. Below is the structure of my "setup" (+ = directory/folder, - = files)

 +Code
    +Classes
         +Fall_11
              +Numerical_Programs
                  +Bisection
                      -.c and .h file(s) for bisection
                       +linux
                            -makefile
    +util
        +CompareFloats
            - CompareFloats.h

So basically, I have Bisection.c and Bisection.h within the directory Bisection. Bisection.c uses floating-point comparison functions that are within CompareFloats.h, in the util/CompareFloats directory.

Below is the makefile I've been trying to use:

###############################################################################
#       makefile for Bisection test                           #
###############################################################################


#--------------------------------Macros----------------------------------------#
COMPILER = gcc
UTIL = ../../../../../util/CompareFloats
FLAGS = -ggdb -Wall -lm -I$(UTIL)
OBJECTS =  ../Bisection.o ../BisectTest.o $(UTIL)/CompareFloats.o
OBJECT_DIR = ../OBJECTS
EXEC = BisectTest

all :   $(EXEC)
    test -d $(OBJECT_DIR) || mkdir $(OBJECT_DIR)
    mv $(OBJECTS) $(OBJECT_DIR)

$(EXEC) : $(OBJECTS)
      $(COMPILER) $(FLAGS) $(OBJECTS) -o $(EXEC)

#-----------------------------Dependencies--------------------------------------#
Bisection.o :       ../Bisection.h
BisectTest.o:       ../Bisection.h 
CompareFloats.o:    $(UTIL)/CompareFloats.h

clean:
    rm -f $(EXEC) *.o $(OBJECT_DIR)/*.o
    rmdir $(OBJECT_DIR)

I understand that the -I flag will allow header files to be searched for in other directories. I figured I was using it correctly, but I can't get it to work. I just get an error that CompareFloats.h does not exist (I've double checked the path and it is correct). Inside Bisection.c, I have

#include "CompareFloats.h"

like usual. The only way I can get this to work is to have the #include statement contain the relative path, like

#include "../../../../util/CompareFloats/CompareFloats.h"

(it has one less ../ because Bisection.c is one level up from the makefile).

Any suggestions? I'd really like to just have #include "CompareFloats.h" instead of the relative path.

If I am not providing enough information, please let me know.

Thanks.

Upvotes: 1

Views: 7800

Answers (2)

Joshua
Joshua

Reputation: 43317

Start with:

CFLAGS=$(CFLAGS) -I$(UTIL)
CXXFLAGS=$(CXXFLAGS) -I$(UTIL)

Most people also end up needing

LDFLAGS=$(LDFLAGS) -I$(UTIL)

but you don't yet.

Upvotes: 0

Foo Bah
Foo Bah

Reputation: 26271

So your problem is in the auto rule generated for Bisection.o. You can do one of two things:

1) make a rule for the Bisection.o:

Bisection.o :       ../Bisection.h
        gcc -c -o ../Bisection.o -I$(UTIL) ../Bisection.c 

2) append the include flag -I$(UTIL) to the variable CFLAGS (these are the flags passed to the C compiler)

Upvotes: 1

Related Questions