Makefile运行不止一次

时间:2012-09-05 17:08:59

标签: java makefile

我正在尝试使用此Makefile编译我的Java代码。但是当我运行make时,它不止一次编译代码。

生成文件:

.PHONY: all clean run

SOURCES = \
    Models/CustomException \
    Models/Forest \
    Models/ForestContainer \
    Models/Owner \
    Models/OwnerContainer \
    Models/Tree


SRC = $(addprefix src/, $(addsuffix .java, $(SOURCES)))
BIN = $(addprefix bin/, $(addsuffix .class, $(SOURCES)))

all: ${BIN}

clean:
    -rm -f ${BIN};

${BIN}: ${SRC}
    mkdir -p bin/Controllers;
    mkdir -p bin/Models;
    mkdir -p bin/Views;
    javac -d bin ${SRC};

run: all
    java -cp bin Program;

输出:

┌─( klim ) - ( ~/workspace/Forest-mvc )
└─> make

mkdir -p bin/Controllers;
mkdir -p bin/Models;
mkdir -p bin/Views;
javac -d bin src/Models/CustomException.java src/Models/Forest.java src/Models/ForestContainer.java src/Models/Owner.java src/Models/OwnerContainer.java src/Models/Tree.java;
mkdir -p bin/Controllers;
mkdir -p bin/Models;
mkdir -p bin/Views;
javac -d bin src/Models/CustomException.java src/Models/Forest.java src/Models/ForestContainer.java src/Models/Owner.java src/Models/OwnerContainer.java src/Models/Tree.java;
mkdir -p bin/Controllers;
mkdir -p bin/Models;
mkdir -p bin/Views;
javac -d bin src/Models/CustomException.java src/Models/Forest.java src/Models/ForestContainer.java src/Models/Owner.java src/Models/OwnerContainer.java src/Models/Tree.java;
mkdir -p bin/Controllers;
mkdir -p bin/Models;
mkdir -p bin/Views;
javac -d bin src/Models/CustomException.java src/Models/Forest.java src/Models/ForestContainer.java src/Models/Owner.java src/Models/OwnerContainer.java src/Models/Tree.java;
mkdir -p bin/Controllers;
mkdir -p bin/Models;
mkdir -p bin/Views;
javac -d bin src/Models/CustomException.java src/Models/Forest.java src/Models/ForestContainer.java src/Models/Owner.java src/Models/OwnerContainer.java src/Models/Tree.java;
mkdir -p bin/Controllers;
mkdir -p bin/Models;
mkdir -p bin/Views;
javac -d bin src/Models/CustomException.java src/Models/Forest.java src/Models/ForestContainer.java src/Models/Owner.java src/Models/OwnerContainer.java src/Models/Tree.java;

正如您所看到的,它不止一次地运行命令,实际上是6次。运行命令的次数随着SOURCES的添加/删除次数的增加/减少而增加/减少

这怎么可能?

3 个答案:

答案 0 :(得分:1)

只需在javac之后添加主类。

${BIN}: ${SRC}
    javac -d bin -cp . Models/Tree.java

将自动编译依赖于主类的所有类和文件夹(包)。你的make一次又一次地编译一些已经编译过的类。

答案 1 :(得分:0)

Makefile all中的第一个目标取决于6个不同的子目标。据推测,在运行开始时,这些都不存在,因此make认为需要构建这6个子目标中的每一个。但是,构建这些子目标中的任何一个实际上都按照您定义的方式构建了所有6个子目标 - 但是make并不知道。

最重要的是,它正在做你告诉它的事情。你告诉它要做的事情多余并不重要make

答案 2 :(得分:0)

对于每个目标int $ {BIN} make将调用$ {BIN}:$ {SRC}规则,但是您在命令行上为javac提供了所有源代码。您可能希望将$ {SRC}替换为$<在javac线上。