4

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 delete the *.o files as last command. I don't 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 anything wrong with my Makefile?

Gremlin
  • 228
  • 1
  • 11
Zhen
  • 4,171
  • 5
  • 38
  • 57

2 Answers2

5

Intermediate files are deleted by design: see Chained Rules in GNU make manual.

Use .SECONDARY or .PRECIOUS targets to keep your precioussss temp files.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • I hit such problem but only .PRECIOUS did work not .SECONDARY ( see http://stackoverflow.com/questions/27090032/why-make-remove-intermediate-file-even-with-secondary-and-require-to-use-preci ), if you mind taking a look... – philippe lhardy Nov 23 '14 at 16:57
3

Just to clarify the previous response, you need to add a special rule like

.PRECIOUS: myfile.o
Gremlin
  • 228
  • 1
  • 11