Makefile removes object files for no reason

13,914

Solution 1

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)

Solution 2

Just add:

.PRECIOUS: %.o

This will tell make that intermediate files are also precious

Share:
13,914
user2112840
Author by

user2112840

Updated on June 15, 2022

Comments

  • user2112840
    user2112840 almost 2 years

    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
    
  • Oliver Charlesworth
    Oliver Charlesworth about 11 years
    Yes, you're absolutely right. Although evidently this isn't the only problem (see my comment to the OP regarding g++ vs. gcc).
  • Estecka
    Estecka about 3 years
    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.