I have the following makefile snippet to compile my C++ project.
obj/%.o: src/%.cpp
$(CXX) $(CFLAGS) -c $< -o $@
Now I want to link the .o
files. But I want to be able to just call the rule name of the link, like make build/main
, in order to compile AND link.
Neither this:
build/main: $(wildcard obj/*.o)
$(CXX) $^ -o $@
works, as it only links and does not compile, as I would expect by this answer;
nor this:
build/main: obj/%.o
$(CXX) $^ -o $@
with the error:
No rule to make target 'obj/%.o', needed by 'build/main'. Stop.
even though it is needed.