I have a simple project that looks something like this
.
├── build
│ ├── file1.o
│ └── one
│ ├── file1.o
│ └── file2.o
├── .depend
├── Makefile
└── src
├── file1.cpp
└── one
├── file1.cpp
└── file2.cpp
The Makefile is something like this:
# Get all of the source files
SRC = $(shell find src/ -name "*.cpp")
# Get all of the object files
OBJ = $(subst src,build,$(SRC:.cpp=.o))
$(OBJ):
@mkdir -p $(shell dirname $@)
g++ -g -c $(subst build,src,$(subst .o,.cpp,$@)) -o $@
all: depend build
build: $(OBJ)
gcc -o project $^
depend:
g++ -MM $(SRC) > .depend
sed -i 's/.\/src/.\/build\//g' .depend
sinclude .depend
I am attempting to generate makefile dependencies by running g++ -MM src/file1.cpp src/one/file1.cpp src/one/file2.cpp > .depend
, and it generates the following directives:
file1.o: src/file1.cpp <other headers>
file1.o: src/one/file1.cpp <other headers>
file2.o: src/one/file2.cpp <other headers>
The problem with this, is that build/file1.o
does not match file1.o
, and as a result, changing src/file1.cpp
or any of the headers it depends on does not cause the object file to be rebuilt. At first I thought it might have been an issue where sinclude .depend
was run before the .depend file was generated, but the problem persists even if I run make depend
followed by make build
. From everything I've read, there are no g++
arguments or options that would preserve the path of the name.
Is it possible to generate a dependency file this way, or is this a fundamentally incorrect approach to building a project?
I took a look at the answers for the question this question was marked as a possible duplicate of, but it seems that question is asking how to create a complete makefile for a project, whereas my issue is not with the creation of a Makefile, but rather an issue with gcc -MM
dependency generation. The answers to that question do not address my problems.