Bob
Bob

Reputation: 1237

makefiles and linking a library in a different folder

I've search around a bit on StackOverflow and tried a few suggestions but as of yet nothing has solved the problem.

I'm making a makefile for a school project and as part of my project I'm generating a static library and linking against it. The compiler throws an error when it gets to a header include in the static library. The code for that is just #include "StringUtil.h"

So in the makefile I have these relevant parts of code

LINKFLAGS=-Llib/ -lHTMLtools

bin : lib $(BIN_FILE)
lib : $(LIB_OBJ_FILES)
    ar r lib/libHTMLtools.a $(LIB_OBJ_FILES)

$(BIN_FILE) : $(OBJ_FILES) #This is only obj/crawler.o for now
    g++ -o bin/crawler obj/crawler.o
obj/crawler.o : src/crawler.cpp inc/crawler.h
    g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp

so whenever I run the make bin command it generates lib.libHTMLtools.a as expected but when it gets to the
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
line it returns this error.

src/crawler.cpp:2:24: fatal error: StringUtil.h: No such file or directory compilation terminated.

Any help or advice will be appreciated!

Upvotes: 2

Views: 4096

Answers (1)

crazyjul
crazyjul

Reputation: 2539

In C++, library files are not enough. They are not used when compiling source code, but only when linking. To compile source file, you need to include headers. But the compiler need to know where to find it. Try adding -I utils/inc to your last line like this

g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc -I utils/inc src/crawler.cpp

Upvotes: 4

Related Questions