Makefile and include

Based on answers, thank you, I made this script:

CPP = $(shell find "cpp/" -name "*.cpp")

DFILE = dependence.d
Programm: $(patsubst %.cpp, %.o, $(CPP))
    g++ -MM $(CPP) >$(DFILE)
    g++ -o Programm $(patsubst %.cpp, %.o, $(CPP))
%.o: %.cpp
    g++ -c -o $@ $^
include $(DFILE)

But it does not keep track of the dependencies that are in the DFILE, and they are there, the generation is correct. How do I use include incorrectly?

1 Recognize all files in cpp/*. cpp

3 Declare a variable to store the dependency file name DFILE = dependency.d

4 Programm goal, rename cpr to o in the dependency line

5 File generation dependencies

6 Build based on the prescribed dependencies on the 6th line, all dependencies, for the purpose, will be executed according to the rule on the 10th line

Tk during the first build, the file is empty DFILE - there are no additional dependencies.

In the second build, I changed SubClass. h, all these actions are repeated and the dependencies created during the last build should be taken into account - the dependency file is not empty, but I can't do this. PS: I will make a separate goal in progress Dependency, for updating dependencies, but I would like to deal with this. Dependency file:

SubClass.o: cpp/SubClass.cpp cpp/../h/SubClass.h \
 cpp/../h/../h/SuperClass.h
SuperClass.o: cpp/SuperClass.cpp cpp/../h/SuperClass.h
Main.o: cpp/Main.cpp cpp/../h/SuperClass.h cpp/../h/SubClass.h \
 cpp/../h/../h/SuperClass.h
Author: Дух сообщества, 2015-06-29

1 answers

  1. I would recommend to abandon the idea of putting files in directories depending on their suffix: I think this is a bad idea.
  2. but if "very necessary", then here is makefile, which hopefully does exactly what is required:

CPP = $(wildcard cpp/*.cpp)
objects = $(patsubst cpp/%.cpp,%.o,$(CPP))
DFILE = dependence.d

Programm: $(objects); g++ -o $@ $^

$(DFILE):; g++ -MM $(CPP) >$(DFILE)

%.o: cpp/%.cpp; $(COMPILE.cpp) $(OUTPUT_OPTION) $<

include $(DFILE)

First you need to create a file with dependencies:

$ make dependence.d

And then everything else:

$ make
 1
Author: aleksandr barakin, 2015-06-29 13:55:56