I’ve tried numerous attempts to move my .o files to my obj folder, but no matter what I do, it simply just doesn’t work.
Judging from the makefile provided, what is the best method to move .o files to a specified folder?
BIN = bin/
OBJ = obj/
TARGET = opengl_03
DEPS = main.o displayinit.o initializer.o algorithms.o matrix3f.o window.o vertex3.o
CC = g++
CFLAGS = -g
LIBS = -lglut -lGLEW -lGL
INCLUDEPATH = -L/usr/include/ -L/usr/lib/ -L/usr/lib/x86_64-linux-gnu/
$(TARGET) : $(DEPS)
$(CC) $(CFLAGS) -o $(BIN)$(TARGET) $(DEPS) $(LIBS) $(INCLUDEPATH)
displayinit.o : displayinit.cpp displayinit.h
$(CC) $(LIBS) $(INCLUDEPATH) -c displayinit.cpp && mv displayinit.o $(OBJ)displayinit.o
initializer.o : initializer.cpp initializer.h
$(CC) $(LIBS) $(INCLUDEPATH) -c initializer.cpp $(OBJ)
algorithms.o : algorithms.cpp algorithms.h
$(CC) -c algorithms.cpp $(OBJ)
matrix3f.o : matrix3f.cpp matrix3f.h
$(CC) $(LIBS) $(INCLUDEPATH) -c matrix3f.cpp $(OBJ)
vertex3.o : vertex3.cpp vertex3.h
$(CC) $(LIBS) $(INCLUDEPATH) -c vertex3.cpp $(OBJ)
window.o : window.cpp window.h
$(CC) $(LIBS) $(INCLUDEPATH) -c window.cpp $(OBJ)
main.o : main.cpp
$(CC) $(LIBS) $(INCLUDEPATH) -c main.cpp $(OBJ)
To specify where the object is created use
-oHere is what you could do:
specify the directory where you want the object files to go
Create a list of object files that need to be compiled, from the list of all
.cppfiles by replacing.cppwith.oand add the prefix$(OBJDIR)/to it:So your $(OBJ) will look like:
objdir/window.o objdir/main.oand so onAdd a target to create the directory if it does not exist:
Make the directory target before you make your main target:
Rule to compile all the
.oobject files in$(OBJDIR)from.cppfiles in the current directory:This will result in something like:
Your main target is unchanged:
This will look like:
For completeness add
cleantarget to cleanup objects:And define
.PHONYtargets, e.g. these will be made even if directories or files with the same name exist:So it should look like:
And if you have files such as
main.cppanda.cppthis is whatmakewould do:And if you want to read more details about any of the above have a look at GNU make doc page