I’ve been trying for over an hour to fix my makefile but I break more than I fix. Anyhow, I’m pretty (very) new to Makefiles and I have little to no experience. My problem is that my makefile recompiles every single source file even if the object files already been built (and the source haven’t been updated). Since makefile is quite like declarative programming I have no idea what I am doing wrong or right.
Sorry for the short description buy my knowledge in this area is very limited…
#---------------------------------------------------------
# Compiler and linker flags
#---------------------------------------------------------
CC = g++
CMNFLAGS = -ggdb3 -Wextra -Wall -Wno-int-to-pointer-cast -Wno-reorder -Wno- write-strings -DOPT_TYPE="\"debugging\""
CFLAGS = $(CMNFLAGS) -fPIC
LDFLAGS = $(CMNFLAGS) -shared -ldl -lm -static-libgcc
#---------------------------------------------------------
# All the different directories
#---------------------------------------------------------
BUILD = build
SOURCE = source
INCLUDE = include
TARGET = $(BUILD)/$(shell basename $(CURDIR))_mm_i386.so
#---------------------------------------------------------
# Directory and include variables/files
#---------------------------------------------------------
SRCSDK = ../sdk
METADIR = $(INCLUDE)/metafiles
INSTDIR = /usr/local/test/$(shell basename $(CURDIR))/dlls
INCLUDES = -I$(INCLUDE) -I$(METADIR) -I$(SRCSDK)/engine -I$(SRCSDK)/common \
-I$(SRCSDK)/pm_shared -I$(SRCSDK)/dlls -I$(SRCSDK)
#---------------------------------------------------------
# Special variables
#---------------------------------------------------------
VPATH = $(SOURCE) $(foreach dir, $(MODULES), $(SOURCE)/$(dir))
#---------------------------------------------------------
# Source and object files
#---------------------------------------------------------
MODULES = game
SRC_DIR = $(addprefix $(SOURCE)/, $(MODULES))
SOURCES = $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.cpp))
SOURCES += $(wildcard $(SOURCE)/*.cpp)
OBJECTS = $(foreach dir, $(SOURCES:.cpp=.o), $(BUILD)/$(notdir $(dir)))
#---------------------------------------------------------
# All the makefile directives
#---------------------------------------------------------
.PHONY: clean install
all: $(SOURCES) $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
$(BUILD)/%.o: %.cpp $(BUILD)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ -c $<
$(BUILD):
@mkdir $@
clean:
-rm -r $(BUILD)
@echo clean...
install: $(TARGET)
@cp $(TARGET) $(INSTDIR)
@echo installation done...
should be
Your line says that your object files are built from the directory which contains them. Almost any action will update the timestamp of your build directory, so you objects are almost always out of date.