GNU使不删除中间文件 [英] GNU make Not Deleting Intermediate Files

查看:117
本文介绍了GNU使不删除中间文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的makefile如下:

My makefile is as follows:

# 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文件指定为中间文件.但是,它们不会在编译完成后按预期方式删除.相反,它们保留在创建位置,使我的源目录混乱不清.

As you can see, the .o files are specified as intermediates via the .INTERMEDIATE target. However, they are not deleted as expected after compilation finishes. Instead, they remain where they were created, cluttering up my source directory.

奇怪的是,它可以完美地在另一台机器上运行.这使我相信它是make的不同版本,但是man make仍将其显示为"GNU make实用程序".

The strange thing is that it works perfectly on another machine. This leads me to believe it's a different version of make, but man make still shows it as the "GNU make utility."

为什么不make删除中间文件?

Why won't make delete the intermediate files?

make -v报告版本3.81.

手动删除.o文件(即干净的文件)后,make All产生以下输出:

After manually deleting the .o files (i.e., a clean slate), make All produces the following output:

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

推荐答案

所以我将其复制到了我的机器上,并设法重现了您的问题和解决方案.

So I copied this onto my machine and managed to reproduce both your problem and the solution.

请注意,在您的.INTERMEDIATE目标中,您必须使用$(OBJECT_FILES)作为前提条件,但是对于制作.o文件的规则,则使用模式规则.这使make感到困惑,并且无法识别两者都指的是同一件事.有两种方法可以解决此问题:

Notice that in your .INTERMEDIATE target, you use $(OBJECT_FILES) as a prerequisite, but for the rule that makes .o files you use a pattern rule. This confuses make and it doesn't recognize that the both refer to the same thing. There are two solutions to this problem:

  1. .INTERMEDIATE的先决条件从$(OBJECT_FILES)更改为%.o,看起来像

  1. Change the prerequisites of .INTERMEDIATE from $(OBJECT_FILES) to %.o, so it looks like

.INTERMEDIATE: %.o

  • 将制作.o文件的规则更改为

  • Change the rule for making .o files to

    $(OBJECT_FILES): $(SOURCE_FILES)
        $(COMPILER) $(COMPILER_OPTIONS) -c $< -o $@
    

    或类似的东西.

    我建议第一种解决方案,因为如果您有多个源文件,则不太可能在编译时引起奇怪的问题.

    I recommend the first solution, as it's less likely to cause weird problems with compilation if you have several source files.

    有关中间目标的更多信息,请参见这里.

    More info on the intermediate target can be found here.

    这篇关于GNU使不删除中间文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆