c++ - Why can I not include this file correctly using a makefile? -
my directory structure looks this:
root |____sg | | | |____makefile | |____simple_client_main.cpp | |___eee |___my_utils.h sg base of operations building "simple_client", , i'm running make here. in simple_client_main.cpp have following #includes:
#include <iostream> #include <string> #include "my_utils.h" so need makefile know my_utils.h is. in mind, want add root/eee directory include directory. (from am, ../eee.)
following advice suggested here, makefile looks this:
dir1 = ../eee cxxflags = $(flag) objs = simple_client_main.o srcs = simple_client_main.cpp all: simple_client simple_client: $(objs) g++ -o simple_client -i$(dir1) $(objs) -lz # [...] depend: makedepend -- $(cflags) -- $(srcs) but doesn't work:
simple_client_main.cpp:6:25: fatal error: my_utils.h: no such file or directory compilation terminated. note if manually set #include directive in cpp follows:
#include "../eee/my_utils.h" ...everything works expected.
what doing wrong here?
you need add -i$(dir1) either cflags or cxxflags (or perhaps both), when object file compiled, option present in compiler command line.
you want make execute similar to:
g++ -c -i../eee simple_client_main.cpp it should if add -i../eee $(cxxflags) or $(cflags). need know rules used make program you're using — can vary.
when linking object files, late -i option of relevance (but should still include $(cflags) or $(cxxflags) in linker command line other options, notably -g, of relevance when linking when compiling object code).
here simple modifications outline makefile shown in question.
dir1 = ../eee iflags = -i$(dir1) cxxflags = $(flag) $(iflags) cflags = $(iflags) ldflags = ldlibs = -lz cxx = g++ objs = simple_client_main.o srcs = simple_client_main.cpp all: simple_client simple_client: $(objs) $(cxx) -o $@ $(cxxflags) $(objs) $(ldflags) $(ldlibs) a makefile stands modest chance of working correctly. not clear might put in flag macro, i've left it. (i use uflags , uxxflags 'user-defined c (or c++) flags'; can set on command line , never set makefile , included in cflags or cxxflags — may after similar.)
note how linking line macros. normal , desirable; macros can changed when running make without editing makefile, constant text cannot changed without editing makefile. -c , -o options c , c++ compilers should ever appear plain text.
if there still problems, @ built-in rule compiling c++ source code object file, , tweak definitions accordingly. (you can use make -p print rules — may need find out going on, hope not sake because tend complex. using make -f /dev/null -p shows built-in rules only; can useful, too.)
note make depend rule may need surgery. uses $(cflags). if $(cxxflags) contains options needed makedepend command, may need instead, or well. if have c++ source, need $(cxxflags) macro in command line.
Comments
Post a Comment