'Makefile:I changed the directory of objs ,but all the files are compiled everytime

I want to save all the generated .o files under the folder $(BUILD_DIR).

Although I did not edit any flies, all the src files will be recompiled.

Thanks to @JohnBollinger 's suggestion. I created a new project to test the problem.

My test project directory tree:

/test
   /app
      main.c
      Makefile
   /build_dir
      Makefile
   /bin
   Makefile


This is my top Makefile


TOPDIR := $(PWD)
CROSS_COMPILE :=


AS      = $(CROSS_COMPILE)as
LD      = $(CROSS_COMPILE)ld
CC      = $(CROSS_COMPILE)gcc
CPP     = $(CC) -E
AR      = $(CROSS_COMPILE)ar
NM      = $(CROSS_COMPILE)nm
STRIP       = $(CROSS_COMPILE)strip
OBJCOPY     = $(CROSS_COMPILE)objcopy
OBJDUMP     = $(CROSS_COMPILE)objdump

MARCH :=x86

SZ = $(CROSS_COMPILE)size

OPT = -O0
CFLAGS+= $(C_INCLUDES)
CFLAGS+= -g -gdwarf-2
CFLAGS+= $(OPT)

TARGET = test-$(MARCH)
BUILD_DIR = $(TOPDIR)/build_dir
BIN_DIR = $(TOPDIR)/bin


SUBDIRS = $(TOPDIR)/app \

export  CC CPP LD MARCH CFLAGS LDFLAGS TARGET CROSS_COMPILE
export BUILD_DIR BIN_DIR TOPDIR LINK_SCRIPT
export CPU ARCH MARCH BOARD

ALL:$(SUBDIRS)
    $(OBJCOPY) -O binary -S $(BIN_DIR)/$(TARGET).elf $(BIN_DIR)/$(TARGET).bin
    @echo  "\033[32mbuild success \033[0m"




$(SUBDIRS):ECHO
    @make clean
    @-mkdir -p bin
    @for dir in $(SUBDIRS); do \
    echo -------------------compiling $$dir ------------------;\
    make -C $$dir || exit "$$?";\
    done
    @make -C $(BUILD_DIR)


ECHO:

.PHONY : clean ALL
clean:
    -rm $(BUILD_DIR)/*.o 
    -rm $(BIN_DIR)/*

here is my Makefile in app directory:

objs += $(BUILD_DIR)/main.o

all:$(objs)

$(BUILD_DIR)/%.o:%.c
    $(CC) $(CFLAGS) -c -o $@ $< -MD -MF [email protected]

.PHONY: all

I tried make -d,and spotted:

 Considering target file '/home/alan/Desktop/test/build_dir/main.o'.
   File '/home/alan/Desktop/test/build_dir/main.o' does not exist.
   ...
   No need to remake target 'main.c'.
   Finished prerequisites of target file '/home/alan/Desktop/test/build_dir/main.o'.
  Must remake target '/home/alan/Desktop/test/build_dir/main.o'.

Actually:

alan@alan-PC:~/Desktop/test/build_dir$ ls
main.o  main.o.d  Makefile

The main.o exist!

Why make think it doesn't exist?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source