尽管配方“全部”被标记为伪造,但每次都会重建多个程序的Makefile

The problem is as described in the title. Every time i type make, even if no chagnes are made to the code, these commands are being run

g++ -o ./bin/prog1 ./obj/prog1.o -I./include
g++ -o ./bin/prog2 ./obj/prog2.o -I./include

Makefile is located at the root of the project folder and the rest of the files are in the include, obj, src and bin folders respectively. Here is the Makefile code:

IDIR=./include
ODIR=./obj
SRCDIR=./src
BINDIR=./bin

CC=g++
CFLAGS=-I$(IDIR)

_DEPS = 
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ1 = prog1.o
OBJ1 = $(patsubst %,$(ODIR)/%,$(_OBJ1))

_OBJ2 = prog2.o
OBJ2 = $(patsubst %,$(ODIR)/%,$(_OBJ2))

_SRC = prog1.cpp prog2.cpp
SRC = $(patsubst %,$(SRCDIR)/%,$(_SRC))

$(ODIR)/%.o: $(SRCDIR)/%.cpp $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

all: $(OBJ1) $(OBJ2)
    $(CC) -o $(BINDIR)/prog1 $(OBJ1) $(CFLAGS)
    $(CC) -o $(BINDIR)/prog2 $(OBJ2) $(CFLAGS)

clean:
    rm -f $(ODIR)/*.o $(BINDIR)/*

.PHONY: all clean

任何帮助,将不胜感激。

评论
  • qquas
    qquas 回复

    Marking all as .PHONY means that the target will always be considered dirty and its recipe will be re-evaluated. If you don't want that, move the two programs into explicit targets:

    $(BINDIR)/prog1: $(OBJ1)
        $(CC) -o $@ $^ $(CFLAGS)
    
    $(BINDIR)/prog2: $(OBJ2)
        $(CC) -o $@ $^ $(CFLAGS)
    
    all: $(BINDIR)/prog1 $(BINDIR)/prog2
    

    Now remaking the all target will ensure prog1 and prog2 are up to date, but run no further commands.