简体   繁体   English

Java Makefile始终会重建,即使没有进行任何更改

[英]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编译器, XcYofoo.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 . 根据您的情况,如果您查看javacA.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.

相关问题 无法编译.java代码,未进行任何更改 - Can't compile .java code, no changes were made 即使对另一个 LinkedHashMap 进行了更改,Groovy 中的并发异常 - Concurrent exception in Groovy even when changes are made to another LinkedHashMap 进行更改后如何保存JTable - How to save a JTable after changes were made 有意通过了ArrayList但未进行任何更改? - Passed ArrayList in an Intent but No Changes Were Made? 如何使用ormlite跟踪数据库已更改? - How to track with ormlite that database changes were made? 有没有一种方法可以简单地侦听对象的更改,而不是进行哪些更改? - Is there a way to simply listen for changes to an object, not what changes were made? 在连续 java 之间对共享上下文 object 所做的最新更改是否对执行 lambda 的每个线程始终可见 - Are latest changes made to shared context object between consecutive java CompletionStages always visible to each thread executing the lambda 如何保存在函数内部进行的数组更改? - How to save array changes that were made inside function? 如何监视文件修改并了解所做的更改 - How to monitor file modifications and know what changes were made 在Java中对对象进行的更改 - Changes being made to an object in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM