I have the following makefile
CXX = g++
CXXFLAGS = -c -g -pg -Wall -Wextra
LINK = g++
TARGET = ../../Binaries/tests
SOURCES := ../Src/$(wildcard *.cpp)
OBJS_DIR := ../Objects
OBJS = $(sort $(patsubst %.cpp, $(OBJS_DIR)/%.o, $(patsubst %.c, $(OBJS_DIR)/%.o, $(notdir $(SOURCES)))))
tests: $(TARGET)
$(TARGET): $(OBJS)
$(LINK) $? -o $@
$(OBJS): $(SOURCES)
$(CXX) $(CXXFLAGS) $? -o $@
which, when run using make tests (or simply just make), gives the following output:
g++ -o ../../Binaries/tests
g++: fatal error: no input files
compilation terminated.
make: *** [../../Binaries/tests] Error 4
This seems to me that make is trying to link the files (that don’t yet exist) without checking the rule for $(OBJS). My goal is to have a Makefile that is entirely self sufficient, being able to find the *.cpp file in ../Src and the *.o file (that it will generate itsef) in ../Objects, however the rule I’ve written doesn’t seem to be working out that way. Can anyone please tell me where I’ve gone wrong here?
Most likely, the problem is on the following line:
It should be something like this instead:
Also transforming sources into objects could be rewritten as follows:
Finally the compilation rule is usually implemented through pattern rules: