防止makefile重新编译所有源文件

时间:2012-04-30 03:04:02

标签: compilation makefile

我已经尝试了一个多小时来修复我的makefile但是我打破了比我修复更多的东西。无论如何,我对Makefiles非常(非常)新手,我几乎没有经验。我的问题是我的makefile重新编译每个源文件,即使已经构建了目标文件(并且源尚未更新)。因为makefile非常像声明性编程,所以我不知道我做错了什么或者是对的。

对不起,简短说明购买我在这方面的知识非常有限......

#---------------------------------------------------------
# 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...

1 个答案:

答案 0 :(得分:5)

$(BUILD)/%.o: %.cpp $(BUILD)

应该是

$(BUILD)/%.o: %.cpp

你的专栏说你的目标文件是从包含它们的目录构建的。几乎任何操作都会更新构建目录的时间戳,因此对象几乎总是过时的。