My makefile works alright (GNU Make), but I was wondering whether
- There is a way to make a new directory “bin” and put all the .o in there
- If the answer to “1” is “yes”, can the instruction “make clean” delete the folder “bin”?
- Avoid having to write all the .cpp and .o filenames?
CFLAGS = -Wall -g CC = g++ EXEC = main OBJ = listpath.o Parser.o main.o all: $(EXEC) listpath.o: src/listpath.cpp $(CC) $(CFLAGS) -c src/listpath.cpp Parser.o: src/Parser.cpp $(CC) $(CFLAGS) -c src/Parser.cpp main.o: src/main.cpp $(CC) $(CFLAGS) -c src/main.cpp $(EXEC): $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o $(EXEC) .cpp.o: $(CFLAGS) $< .PHONY: clean clean: rm $(OBJ) $(EXEC)
Starting with the last question, you can define implicit rules to convert .c files to .o files.
This will also out the .o files to the bin/ directory.
Now your result rule will look like:
To clean the bin/ directory, simple change your clean rule to:
Finally, add a rule to create the bin directory: