I don't know why the Make recompiles all the files every time including the unchanged files, it takes a long time. I searched the Stack Overflow and found some solutions, but I still couldn't resolve my problem.
How should I change this makefile so that it won't compile all the files including the unchanged files?
This is my make file:
CXX = g++
CXXFLAGS = -g -Wall -O2 -std=c++11
BIN = bin
SRC = src
OBJ = obj
COMPILER = $(BIN)/compiler
LEX_CPP = $(SRC)/lex.cpp
UTIL_CPP = $(SRC)/util.cpp
TOKEN_CPP = $(SRC)/token.cpp
MAIN_CPP = $(SRC)/main.cpp
TEST_CPP = $(SRC)/test.cpp
PARSER_CPP = $(SRC)/parser.cpp
COMPILER_CPP = $(SRC)/compiler.cpp
COMPILER_OBJS = parser.o compiler.o test.o token.o lex.o util.o main.o
dir_guard=@mkdir -p $(OBJ)
compiler: $(COMPILER_OBJS)
$(CXX) $(CXXFLAGS) -o $(COMPILER) $(OBJ)/lex.o $(OBJ)/compiler.o $(OBJ)/parser.o $(OBJ)/test.o $(OBJ)/token.o $(OBJ)/util.o $(OBJ)/main.o
main.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/main.o -c $(MAIN_CPP)
lex.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/lex.o -c $(LEX_CPP)
util.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/util.o -c $(UTIL_CPP)
token.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/token.o -c $(TOKEN_CPP)
test.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/test.o -c $(TEST_CPP)
compiler.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/compiler.o -c $(COMPILER_CPP)
parser.o:
$(dir_guard)
$(CXX) $(CXXFLAGS) -o $(OBJ)/parser.o -c $(PARSER_CPP)
clean:
rm $(OBJ)/*.o
I want to put all the .o files to the obj
folder, and put the exe file to the bin
folder.
My OS is Ubuntu 15.04 and using GNU Make 4.0.
Any help is appreciated, thank you in advance!