I have the following Makefile:
CXX = g++
CXXFLAGS = -Wall -g
SOURCES := divisor.cpp multiplier.cpp
OBJECTS := ${SOURCES:.cpp=.o}
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $< -o $@
%: %.o $(OBJECTS)
$(CXX) $(CXXFLAGS) $@.o -o $@.out
$(OBJECTS): %.o: %.cpp
$(CXX) -c $(CXXFLAGS) $< -o $@
clean:
rm -f *.o
What I want this make file is the following:
If I add a source file called 123.cpp
to the working directory, I want it to generate its object file and then link the compiled sources specified in $(SOURCES)
, this means:
g++ -c -Wall -g 123.cpp
g++ multipler.o divisor.o 123.o -o 123
If multiplier.cpp
or divisor.cpp
has to be generated or updated, I want make to do it.
But I'm failing, because divisor.o
and multiplier.o
are not automatically generated
How may I achieve this?
Edit
Just to clarify, there are two types of source code files in the working directory: divisor.cpp
, multipler.cpp
is one type, and any other file, say, 123.cpp
is the other type. In a sense, divisor.cpp
and multiplier.cpp
are requisites to the other source files.
I want to automate the process of compiling the prerequisites and link them when compiling the other files
g++ -c multiplier.cpp
g++ -c divisor.cpp
g++ -c -Wall -g 123.cpp
g++ multipler.o divisor.o 123.o -o 123