开发者

makefiles and linking a library in a different folder

开发者 https://www.devze.com 2023-04-11 02:42 出处:网络
I\'ve search around a bit on StackOverflow and tried a few suggestions but as of yet nothing has solved the problem.

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 ex开发者_开发知识库pected 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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号