There is a neat utility called makedepend which can go through your
source code and see which files #include
which other ones. It
then tacks a bunch of dependency lines to the bottom of your makefile
with a commented line that says ``Do not delete this line.'' You can
use it again later to update those dependency lines.
Makedepend is used like this:
makedepend [options] source-files...
For options, -Idirectory
tells it to look in directory
for include files, and -Ydirectory
tells it not to look in
directory. Just -Y
by itself means not to look at any of the
system include directories. You can give as many -I
and -Y
options as you need. Makedepend can ignore any options it doesn't
understand if you put them between double dashes (--
); this is
useful as you'll see below.
If you give it -Y
, it will probably spit out a bunch of stuff
about not being able to find iostream.h
and so on. Just ignore
these messages; they are harmless. Note that you could get rid of
them by omitting -Y
, but then your makefile would have all sorts
of long rules stating that foo.o
depends on iostream.h
and
ios.h
and streambuf.h
and strbuf.h
and .... Since
those system header files almost never change, you don't want make to
waste time scanning them, so you generally leave in the -Y
.
After makedepend scans your source code, it tacks the resulting dependency rules onto the end of your makefile and saves you a ton of typing. It's very useful to have a ``depend'' target in your makefile which does this automatically:
depend: -> makedepend -- $(CXXFLAGS) -- -Y $(SRC_FILES)
We give makedepend the CXXFLAGS
because that macro contains the
-I
commands which are normally passed to g++ to tell it where the
various .h
files are, but those same commands are useful here.
Makedepend happily ignores any other options in CXXFLAGS
such as
debugging flags because of the double dashes --
.
(Problems and suggestions, in increasing order of unlikeliness.)
gmake
?
gmake clean gmake depend gmake
-I
option out somewhere? Remember that C compilation
uses the macros CC
and CFLAGS
and that C++ compilation uses
CXX
and CXXFLAGS
.
.o
file or perhaps a library? Are you doing templates correctly?
(That's another whole story...) Another tricky point is that the
built in rule for linking of .o
files uses the macro CC
,
which defaults to gcc. If you use gcc to link C++ object files, it doesn't
know to add in the C++ library, which has the code for ostreams and
such. The quickest fix is to make sure that you do this:
CXX = g++ CC = $(CXX)
You're more likely to notice this if you make a typo or forget the name of your variable:
PRYNTER = teerlp2 # OOPS. PRINT_COMMAND = print print: -> $(PRINTCMD) -p$(PRINTER) $(FILES) # OOPS.
gmake clean
.
printout: $(SRC) $(HEADERS) -> setenv PRINTER teerlp2 -> print -p$PRINTER $WHATEVER ... #OOPS.If you need to do anything that fancy, write a shell script instead, and have make call it.