我的makefile如下:
# The names of targets that can be built. Used in the list of valid targets when no target is specified, and when building all targets.
TARGETS := libAurora.a libAurora.so
# The place to put the finished binaries.
TARGET_DIRECTORY := ./Binaries
# The compiler to use to compile the source code into object files.
COMPILER := g++
# Used when compiling source files into object files.
COMPILER_OPTIONS := -I. -Wall -Wextra -fPIC -g -O4
# The archiver to use to consolidate the object files into one library.
ARCHIVER := ar
# Options to be passed to the archiver.
ARCHIVER_OPTIONS := -r -c -s
SOURCE_FILES := $(shell find Source -type f -name *.cpp)
OBJECT_FILES := $(SOURCE_FILES:.cpp=.o)
.PHONY: Default # The default target, which gives instructions, can be called regardless of whether or not files need to be updated.
.INTERMEDIATE: $(OBJECT_FILES) # Specifying the object files as intermediates deletes them automatically after the build process.
Default:
@echo "Please specify a target, or use \"All\" to build all targets. Valid targets:"
@echo "$(TARGETS)"
All: $(TARGETS)
lib%.a: $(OBJECT_FILES)
$(ARCHIVER) $(ARCHIVER_OPTIONS) $(TARGET_DIRECTORY)/$@ $(OBJECT_FILES)
lib%.so: $(OBJECT_FILES)
$(ARCHIVER) $(ARCHIVER_OPTIONS) $(TARGET_DIRECTORY)/$@ $(OBJECT_FILES)
%.o:
$(COMPILER) $(COMPILER_OPTIONS) -c -o $@ $*.cpp如您所见,通过.INTERMEDIATE目标将.o文件指定为中间文件。但是,编译完成后,它们不会像预期的那样被删除。相反,它们仍然留在创建它们的地方,弄乱了我的源目录。
奇怪的是,它可以在另一台机器上完美地工作。这让我相信它是一个不同版本的make,但man make仍然将它显示为"GNU make实用程序“。
为什么make不删除中间文件?
编辑:make -v reports版本3.81。
编辑:在手动删除.o文件(即从头开始)之后,make All会生成以下输出:
g++ -I. -Wall -Wextra -fPIC -g -O4 -c -o Source/File/File.o Source/File/File.cpp
g++ -I. -Wall -Wextra -fPIC -g -O4 -c -o Source/Timer/Timer.o Source/Timer/Timer.cpp
ar -r -c -s ./Binaries/libAurora.a Source/File/File.o Source/Timer/Timer.o
ar -r -c -s ./Binaries/libAurora.so Source/File/File.o Source/Timer/Timer.o发布于 2011-06-29 13:36:02
所以我把它复制到我的机器上,并设法重现了你的问题和解决方案。
注意,在.INTERMEDIATE目标中,您使用$(OBJECT_FILES)作为先决条件,但是对于生成.o文件的规则,您使用模式规则。这会使make混淆,并且它不会认识到这两者指的是同一件事。这个问题有两种解决方案:
.INTERMEDIATE的先决条件从$(OBJECT_FILES)更改为%.o,因此如下所示.INTERMEDIATE:% .o
$(OBJECT_FILES):$(SOURCE_FILES) $(编译器) $(COMPILER_OPTIONS) -c $< -o $@
或者类似的东西。
我推荐第一种解决方案,因为如果您有多个源文件,则不太可能导致奇怪的编译问题。
有关中间目标的更多信息,请访问here。
发布于 2011-06-29 05:46:59
在开始构建项目之前,请确保文件不在那里。医生说得很清楚:
因此,在 make之前不存在的中间文件在 make之后也不存在。
如果这不是问题所在,你应该发布一些make的调试输出。
https://stackoverflow.com/questions/6512914
复制相似问题