Why are my temporary files being deleted?

I have a simple 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}

      

But when I executed it, it deletes the * .o files as the last command. I do not know why:

$ 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

      

Is there something wrong with my Makefile?

+3


source to share


2 answers


Intermediate files are removed by design: see "Chaining Rules" in the GNU manual.



Use .SECONDARY

or .PRECIOUS

purpose to save your jewelry temp files.

+3


source


To clarify the previous answer, you need to add a special rule like



.PRECIOUS: myfile.o

      

+3


source







All Articles