44

Here is the Makefile code:

CC=gcc 
CXX=g++ 
OBJS=OMXComponent.o Event.o hello_jpeg.o JPEG.o OMXCore.o Locker.o Logger.o
BIN=hello_jpeg.bin

# include ../Makefile.include


CFLAGS+=-DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi

LDFLAGS+=-L$(SDKSTAGE)/opt/vc/lib/ -lGLESv2 -lEGL -lopenmaxil -lbcm_host -lvcos -lvchiq_arm -lpthread -lrt -L../libs/ilclient -L../libs/vgfont

INCLUDES+=-I$(SDKSTAGE)/opt/vc/include/ -I$(SDKSTAGE)/opt/vc/include/interface/vcos/pthreads -I$(SDKSTAGE)/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont

all: $(BIN) $(LIB)

%.o: %.c
    @rm -f $@ 
    $(CC) $(CFLAGS) $(INCLUDES) -g -c $< -o $@ -Wno-deprecated-declarations

%.o: %.cpp
    @rm -f $@ 
    $(CXX) $(CFLAGS) $(INCLUDES) -g -c $< -o $@ -Wno-deprecated-declarations

%.bin: $(OBJS)
    $(CC) -o $@ -Wl,--whole-archive $(OBJS) $(LDFLAGS) -Wl,--no-whole-archive -rdynamic

I am not sure why, but suddenly when I am calling my Makefile, it started deleting all the object files !!! It was not deleting before, but lately after running the Makefile, this is what I get in the console window:

 rm OMXCore.o JPEG.o OMXComponent.o Logger.o hello_jpeg.o Locker.o Event.o

Any idea why it is removing my object files? and how to stop Make from deleting it? What is even more strange is that it was not deleting it yesterday, and it all started today, without me doing any upgrades or making any changes!!! Now when I make a small change in one of my src files, now I have to wait a long time for all the other object files to rebuilt again, which is not necessary and it slows me down.

I am making sure that I am in the correct directory, I even gave "make" a complete path, in case if it is picking up a different Makefile

make -f /my_full_path/Makefile

While Makefile was running, I was doing "ls" in a different terminal, and I noticed that I am correctly accumulating object files, as below:

-rw-rw-r-- 1 pi pi   1170 Mar  3 19:28 Makefile
-rw-r--r-- 1 pi pi 482052 Mar  3 19:30 OMXComponent.o
-rw-r--r-- 1 pi pi  19984 Mar  3 19:30 Event.o
-rw-r--r-- 1 pi pi  14688 Mar  3 19:30 hello_jpeg.o
-rw-r--r-- 1 pi pi 162260 Mar  3 19:31 JPEG.o
-rw-r--r-- 1 pi pi 215492 Mar  3 19:31 OMXCore.o
-rw-r--r-- 1 pi pi  76384 Mar  3 19:31 Locker.o
-rw-r--r-- 1 pi pi      0 Mar  3 19:31 Logger.o

But all of a sudden, after everything is built, all the object files get removed !!!

And, here is the output of the Makefile command:

pi@raspberrypi /opt/vc/src/hello_pi/hello_jpeg_v2 $ make -f /opt/vc/src/hello_pi/hello_jpeg_v2/Makefile 
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c OMXComponent.cpp -o OMXComponent.o -Wno-deprecated-declarations
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c Event.cpp -o Event.o -Wno-deprecated-declarations
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c hello_jpeg.cpp -o hello_jpeg.o -Wno-deprecated-declarations
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c JPEG.cpp -o JPEG.o -Wno-deprecated-declarations
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c OMXCore.cpp -o OMXCore.o -Wno-deprecated-declarations
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c Locker.cpp -o Locker.o -Wno-deprecated-declarations
g++ -g -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -fPIC -DPIC -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -Wall -g -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -Wno-psabi -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I./ -I../libs/ilclient -I../libs/vgfont -g -c Logger.cpp -o Logger.o -Wno-deprecated-declarations
g++ -g -o hello_jpeg.bin -Wl,--whole-archive OMXComponent.o Event.o hello_jpeg.o JPEG.o OMXCore.o Locker.o Logger.o -L/opt/vc/lib/ -lGLESv2 -lEGL -lopenmaxil -lbcm_host -lvcos -lvchiq_arm -lpthread -lrt -L../libs/ilclient -L../libs/vgfont -Wl,--no-whole-archive -rdynamic
rm OMXCore.o JPEG.o OMXComponent.o Logger.o hello_jpeg.o Locker.o Event.o
user2112840
  • 555
  • 2
  • 8
  • 13
  • 2
    but I am not doing "rm" !?! how do I make it not remove the files? – user2112840 Mar 03 '13 at 19:20
  • Does it purge the `.o` files on every run of make, even when you do not have a single `.c` or `.cpp` file change ? – Tuxdude Mar 03 '13 at 19:20
  • Are you sure this is the **exact** Makefile that you're running? Can you add the **complete** console output you get when you run Make, starting from a clean? – Oliver Charlesworth Mar 03 '13 at 19:23
  • No it doesn't ! I just checked it. On every single step, it keeps adding the object files. When I do "ls" I can see that my object files keeps adding up. It is at the last stage, WHICH I DON'T KNOW HOW, somehow "rm" is executed !!! – user2112840 Mar 03 '13 at 19:24
  • 2
    Given the order of files in the rm, and the fact that it prints (when this code has an at-sign in it), I highly suspect this is not your actual makefile. Make sure it isn't picking up another one (perhaps called GNUMakefile, or Makefile, or in a different directory, or whatever) – Rob Napier Mar 03 '13 at 19:24
  • yes and I am positive, that it was not happening yesterday!!! I just stated happening today. And I haven't changed a single thing. Except that I ran "ddd" debugger few times, and I am not sure if that affected it somehow! (which still wouldn't make sense) – user2112840 Mar 03 '13 at 19:25
  • When I monitored the object files that are being built, they are in exact same order in my Makefile, and I am sure I am in the right directory. This is what I just got: -rw-rw-r-- 1 pi pi 1170 Mar 3 19:28 Makefile -rw-r--r-- 1 pi pi 482052 Mar 3 19:30 OMXComponent.o -rw-r--r-- 1 pi pi 19984 Mar 3 19:30 Event.o -rw-r--r-- 1 pi pi 14688 Mar 3 19:30 hello_jpeg.o -rw-r--r-- 1 pi pi 162260 Mar 3 19:31 JPEG.o -rw-r--r-- 1 pi pi 215492 Mar 3 19:31 OMXCore.o -rw-r--r-- 1 pi pi 76384 Mar 3 19:31 Locker.o -rw-r--r-- 1 pi pi 0 Mar 3 19:31 Logger.o – user2112840 Mar 03 '13 at 19:33
  • @user2112840: Please edit your question to include the **complete** output when you run Make. – Oliver Charlesworth Mar 03 '13 at 19:42
  • @OliCharlesworth: sure, I just edited my post. – user2112840 Mar 03 '13 at 19:51
  • @user2112840: Thanks. In that case, I'm officially flummoxed! I think at this point, you may need to resort to `make -d` to see what rules Make is running (note: this isn't a pleasant experience...) – Oliver Charlesworth Mar 03 '13 at 20:11
  • @user2112840: Actually, hang on. Something else weird is happening. You've set `CC=gcc`, and your final rule uses `$(CC) -o`, but in the console output, it's saying `g++ -g -o`. Are you **sure** that `include Makefile.include` is commented out? – Oliver Charlesworth Mar 03 '13 at 20:13
  • All, I solved. I had to make a secondary target list in my Makefile. I did it, and it now not removing it anymore. Thanks for the inputs. (link http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Chained-Rules.html#Chained-Rules) – user2112840 Mar 03 '13 at 20:17
  • 2
    @user2112840: That may have fixed it, but it implies that your `%.bin` rule is not being executed. Some other rule is instead... – Oliver Charlesworth Mar 03 '13 at 20:19

2 Answers2

63

The files are being removed because make considers them "intermediate". When make forms a chain of rules to produce a prerequisite, it treats all files created by the intermediate chains as "intermediate" and removes then when the target is created. See Chained Rules in the manual for GNU make.

In your case, you can prevent this in two ways: Get rid of the %.bin rule, which seems wrong anyway, because it says that all .bin files depend on a fixed list of object files, and replace it by

$(BIN): $(OBJS)
        # as before

or mark the object files as "secondary":

.SECONDARY: $(OBJS)
Idelic
  • 14,976
  • 5
  • 35
  • 40
34

Just add:

.PRECIOUS: %.o

This will tell make that intermediate files are also precious

kolypto
  • 31,774
  • 17
  • 105
  • 99
  • 2
    This will not automatically mark _all_ *.o files as precious; I had a makefile that used `main.%.o` as a dependency, make would still remove them using this rule. I had to twist it into `.PRECIOUS: main.%.o` for it to work. – Estecka Apr 27 '21 at 21:01