c++ - Makefile: How to correctly include header file and its directory? -


i have following makefile:

cc=g++ inc_dir = ../stdcutil cflags=-c -wall -i$(inc_dir) deps = split.h  all: lock.o dbc.o trace.o  %.o: %.cpp $(deps)     $(cc) -o $@ $< $(cflags)  clean:     rm -rf *o 

this makefile , 3 source files lock.cpp, dbc.cpp, trace.cpp located in current directory called core. 1 of source file trace.cpp contains line includes header file outside current directory:

//in trace.cpp #include "stdcutil/split.h" 

the header file split.h located @ 1 level above current directory , in subdirectory called stdcutil. that's why added inc_dir = ../stdcutil in makefile. overall directory structure looks following:

root   |___core   |     |   |     |____makefile   |     |____dbc.cpp   |     |____lock.cpp   |     |____trace.cpp   |   |___stdcutil         |___split.h 

but when make it, gives me error:

trace.cpp:8:28: fatal error: stdcutil/split.h: no such file or directory  #include "stdcutil/split.h"                             ^ compilation terminated. <builtin>: recipe target 'trace.o' failed 

why doesn't find header file split.h if specify inc_dir in makefile? how correct this?

these lines in makefile,

inc_dir = ../stdcutil cflags=-c -wall -i$(inc_dir) deps = split.h 

and line in .cpp file,

#include "stdcutil/split.h" 

are in conflict.

with makefile in source directory , -i option should using #include "split.h" in source file, , dependency should be../stdcutil/split.h`.

another option:

inc_dir = ../stdcutil cflags=-c -wall -i$(inc_dir)/..  # ugly! deps = $(inc_dir)/split.h 

with #include directive remain #include "stdcutil/split.h".

yet option place makefile in parent directory:

root   |____makefile   |   |___core   |     |____dbc.cpp   |     |____lock.cpp   |     |____trace.cpp   |   |___stdcutil         |___split.h 

with layout common put object files (and possibly executable) in subdirectory parallel core , stdcutil directories. object, example. this, makefile becomes:

inc_dir = stdcutil src_dir = core obj_dir = object cflags  = -c -wall -i. srcs = $(src_dir)/lock.cpp $(src_dir)/dbc.cpp $(src_dir)/trace.cpp objs = $(obj_dir)/lock.o $(obj_dir)/dbc.o $(obj_dir)/trace.o # note: above unwieldy. # wildcard , patsubt commands come rescue.  deps = $(inc_dir)/split.h # note: above unwieldy. # want use automatic dependency generator.   all: $(objs)  $(obj_dir)/%.o: $(src_dir)/%.cpp   $(cc) $(cflags) -c $< -o $@  $(obj_dir)/trace.o: $(deps) 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -