4

我有一个简单的 Makefile,

.PHONY: clean

PROGRAMS=$(patsubst main%.cpp,example%,$(wildcard main*.cpp))

all: ${PROGRAMS}

GCCVERSION=$(shell gcc -dumpversion)

GLCFLAGS=$(shell pkg-config --cflags gl)
CPPFLAGS=-Wall -O2 ${GLCFLAGS}
ifeq "${GCCVERSION}" "4.5.2"
    CXXFLAGS=-std=c++0x
else
    CXXFLAGS=-std=c++11
endif

GLLIBS=$(shell pkg-config --libs gl)
LIBS=${GLLIBS} -lglut

example%: main%.o shaders.o fileutils.o
    ${CXX} $^ ${LIBS} -o $@

clean:
    rm -f *.o ${PROGRAMS}

但是当我执行它时,它会删除 *.o 文件作为最后一个命令。我不知道为什么:

$ make
g++ -std=c++11 -Wall -O2 -I/usr/include/libdrm    -c -o main01.o main01.cpp
g++ -std=c++11 -Wall -O2 -I/usr/include/libdrm    -c -o shaders.o shaders.cpp
g++ -std=c++11 -Wall -O2 -I/usr/include/libdrm    -c -o fileutils.o fileutils.cpp
g++ main01.o shaders.o fileutils.o -lGL   -lglut -o example01
rm main01.o fileutils.o shaders.o

我的 Makefile 有什么问题吗?

4

2 回答 2

4

中间文件被设计删除:参见GNU make 手册中的链式规则

使用.SECONDARY.PRECIOUS目标来保存您的珍贵临时文件。

于 2013-02-08T15:51:55.770 回答
3

只是为了澄清先前的响应,您需要添加一个特殊规则,例如

.PRECIOUS: myfile.o
于 2014-10-13T12:28:20.033 回答