[英]Java Makefile always rebuilds, even when no changes were made
My makefile always rebuilds the project, even if no changes were made. 我的makefile始终重建项目,即使没有进行任何更改。
How can I fix it? 我该如何解决?
My project structure follows the usual bin/, src/, Makefile pattern. 我的项目结构遵循通常的bin /,src /,Makefile模式。
Makefile: Makefile文件:
# output directory
BIN = bin/
# input directory
SRC = src/
# java compiler
JC = javac
# compile flags
JFLAGS = -d $(BIN) -cp $(SRC)
sourcefiles = $(addprefix $(SRC), \
A.java \
B.java \
C.java)
classfiles = $(sourcefiles:.java=.class)
all: $(classfiles)
%.class: %.java
$(JC) $(JFLAGS) $<
clean:
$(RM) $(BIN)*.class
I made this makefile from examples I found online, but I'm not sure I understand everything being done, so if I could also get an explanation, that would be great :3 我从网上找到的示例中制作了这个makefile,但是我不确定我是否了解所有工作,因此,如果我也能得到解释,那将是很好的:3
In general, make
is not a good fit for Java. 通常,
make
不太适合Java。 Make works best with tools that behave similarly to traditional compilers: they take an input file foo.X
(and maybe some other input files as well) and they generate a single output file foo.Y
. Make的工作方式与传统编译器类似,它们的工作效果最佳:它们采用输入文件
foo.X
(可能还包括其他一些输入文件),并且生成单个输出文件foo.Y
For a C compiler for example X
is c
and Y
is o
( foo.c
compiles to foo.o
). 例如,对于C编译器,
X
为c
而Y
为o
( foo.c
编译为foo.o
)。
Make is hard to use in situations where a single invocation of the compiler generates more than one output file, and it's not simple to use when the name of the output file doesn't relate directly to the name of the input file (in this case you have to write all explicit rules, not pattern rules). 在编译器的一次调用会生成多个输出文件的情况下,很难使用Make,并且在输出文件的名称与输入文件的名称不直接相关的情况下使用它并不简单(在这种情况下)您必须编写所有显式规则,而不是模式规则)。
For Java compilers a single .java
input file can generate multiple different .class
files, and the names of the .class
files are not necessarily related to the name of the .java
file. 对于Java编译单个
.java
输入文件可以产生多个不同.class
文件和名称.class
文件不一定涉及到的名称.java
文件。
In your situation, I'll bet if you look at the output files that javac
is generating for your A.java
file you'll see it's not generating A.class
. 根据您的情况,如果您查看
javac
为A.java
文件生成的输出文件,我会打赌,您会看到它没有生成A.class
。 Because A.class
doesn't exist, make will always try to rebuild it. 由于
A.class
不存在,make将始终尝试重建它。
Oh. 哦。 Also.
也。 You're putting files in different directories.
您将文件放在不同的目录中。 So even if you DO restrict yourself to situations where the names are identical, you have to write your pattern like this:
因此,即使您确实将自己限制在名称相同的情况下,也必须像这样编写模式:
# ... Keep the first part as in your example
classfiles = $(patsubst $(SRC)%.java,$(BIN)%.class,$(sourcefiles))
all: $(classfiles)
$(BIN)%.class : $(SRC)%.java
$(JC) $(JFLAGS) $<
# ... Keep the clean rule as in your example
The patterns %
must be identical; 模式
%
必须相同; if you put things in different directories they're not identical. 如果将内容放在不同的目录中,则它们是不相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.