tags:

views:

40

answers:

1

Here is a simple header file for six different programs. This Makefile used to work just fine, but then I changed the programs to include other implementation files. This Makefile needs to get changed so that if the implementation files change the files that include those implementation files get recompiled.

all: load list show add delete btree
%: %.cpp
    g++ $< -g -o $@
+1  A: 

You can use the -MM option of gcc to create dependency files, and then include those into your Makefile.

TARGETS = load list show add delete btree
all: $(TARGETS)
%: %.cpp
    g++ $< -g -o $@ -MM -MF [email protected]
    sed "s/$@\.o:/$@:/" [email protected] > [email protected]
    -@rm [email protected]

DEPS=$(TARGETS:%=%.d)
-include $(DEPS)

The sed line is present to change the dependency file from load.o: load.c to load: load.c.

Didier Trosset
When I do g++ -MM list.cpp, I get the following output: list.o: list.cpp bt.h insert.c btio.c btutil.c Lab2.cpp. What am I supposed to do with that? It doesn't actually create anything.
Phenom
You don't need to run it directly - the %: %.cpp rule does that. The rule converts that line to a set of .d files that are then included in the makefile
Mark
`-MM` generates this output, and `-MF` redirects it in a file (ending in .dd). The next line runs `sed` to slightly modify, and rename it in `.d`. These last `.d` files are included in the Makefile, and tells make what file depend on which other.
Didier Trosset
When I run make, I get the following error: Makefile:4: *** missing separator. Stop.
Phenom
You have to use real tabs and not spaces to indent the commands.
Didier Trosset
That fixed it but now all that happens is it creates files that don't do anything.
Phenom
These .d files are included in the Makefile (see last line: -include). They are used by make to know about dependencies and to rebuild files when needed.
Didier Trosset