Consider this makefile:
CXX = g++-7
CC = gcc-7
CXXFLAGS = -Wall -Wimplicit-fallthrough -Wextra -g -fstack-protector-all -std=c++11
LDFLAGS =
EXECUTABLE = engine
EXTENSION = cc
SOURCES = $(basename $(shell find . -name '*.$(EXTENSION)'))
.PHONY: all
all: $(EXECUTABLE)
$(EXECUTABLE): $(addsuffix .o, $(SOURCES))
$(CXX) $(LDFLAGS) $^ -o $@
%.d: %.$(EXTENSION)
$(CXX) $(CXXFLAGS) -MM $< -o $@
-include $(addsuffix .d, $(SOURCES))
.PHONY: clean
clean:
find . -name '*.o' -or -name '*.d' -or -name '*~' -delete
This makefile places all the .d and .o files in the "src" folder, where my .cc and .h files are located.
I edited the makefile to place the .d objects inside a "obj" folder, but somehow I now need a .o rule.
This is the edited makefile: it creates the %.d files in the "obj" folder but then fails with this error: 'make: *** No rule to make target 'obj/wireframe.o', needed by 'engine'. Stop.'
CXX = g++-7
CC = gcc-7
$(shell mkdir "obj")
CXXFLAGS = -Wall -Wimplicit-fallthrough -Wextra -g -fstack-protector-all -std=c++11
LDFLAGS =
EXECUTABLE = engine
EXTENSION = cc
SRCDIR = src
OBJDIR = obj
SOURCES = $(basename $(shell find $(SRCDIR) -name '*.$(EXTENSION)' -printf '%f\n'))
.PHONY: all
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJDIR)/$(addsuffix .o, $(SOURCES))
$(CXX) $(LDFLAGS) $^ -o $@
%.d: $(SRCDIR)/%.$(EXTENSION)
$(CXX) $(CXXFLAGS) -MM $< -o $(OBJDIR)/$@
-include $(addsuffix .d, $(SOURCES))
.PHONY: clean
clean:
find . -name '*.o' -or -name '*.d' -or -name '*~' -delete
.o
rule? If you're having trouble, I suggest you start with$(OBJDIR)/%.o: $(SRCDIR)/%.cc
– Beta