java 的 makefile

发布于 2024-09-24 13:31:44 字数 602 浏览 1 评论 0原文

我不明白我的 makefile 做错了什么:

JAVA_SRCS:=$(wildcard tasks/src/*.java)
JAVA_CLASSES=$(subst /src/,/build/,$(JAVA_SRCS:.java=.class))
JFLAGS=-cp jar/octobot.jar -d tasks/build
JC=javac

.SUFFIXES: .java .class

.java.class:
 $(JC) $(JFLAGS) $*.java

default: build

build: $(JAVA_CLASSES)

clean:
 $(RM) tasks/build/*.class

我收到了这个错误:

make: *** No rule to make target `tasks/build/ClickTask.class', needed by `classes'.  Stop.
zsh: exit 2     make

但奇怪的是,当我像这样重写规则 build 时:

build: $(JAVA_SRCS:.java=.class)

没有错误,规则已启动,但每次都会执行时间(而且不正确)

i don't understand what i did wrong with my makefile :

JAVA_SRCS:=$(wildcard tasks/src/*.java)
JAVA_CLASSES=$(subst /src/,/build/,$(JAVA_SRCS:.java=.class))
JFLAGS=-cp jar/octobot.jar -d tasks/build
JC=javac

.SUFFIXES: .java .class

.java.class:
 $(JC) $(JFLAGS) $*.java

default: build

build: $(JAVA_CLASSES)

clean:
 $(RM) tasks/build/*.class

I got this error :

make: *** No rule to make target `tasks/build/ClickTask.class', needed by `classes'.  Stop.
zsh: exit 2     make

But strangely, when i re-write the rule build like this :

build: $(JAVA_SRCS:.java=.class)

no error, the rule is launched but does it every time (and it's not correct)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-10-01 13:31:44

@Dean Povey 是正确的:您不能使用后缀规则来执行此操作,因为它们与源在同一目录中查找。但是,您可以使用 GNU Make 模式规则来执行此操作(并且您已经在 Makefile 中使用了 GNUMake-isms,所以无论如何):

tasks/build/%.class: tasks/src/%.java
        $(JC) $(JFLAGS) 
lt;

但是请注意,make 不适合构建java 源代码作为一个 .java 文件可能会产生许多 .class 文件(例如内部类)。 Automake 解决这个问题的方法是在一次调用 javac 中编译所有内容,并写出一个时间戳文件(例如 echo timestamp > classnoinst.stamp)。然后,任何需要构建的 java 源代码都依赖于该 stamp 文件,并且 make clean 会删除 .stamp 以及 .class 文件。

@Dean Povey is correct: you can't do this with suffix rules, because they look in the same directory as the source. You can, however, do this with a GNU Make pattern rule (and you're already using GNUMake-isms in your Makefile, so whatever):

tasks/build/%.class: tasks/src/%.java
        $(JC) $(JFLAGS) 
lt;

Note, however, that make is ill-suited to building java source as one .java file can result in many .class files (inner classes, for instance). Automake's approach to this problem is to compile everything in a single call to javac and write out a timestamp file (echo timestamp > classnoinst.stamp, for example). Then anything that needs the java sources built depends on that stamp file and make clean removes the .stamp along with the .class files.

水中月 2024-10-01 13:31:44

我认为这是因为你的 src 位于不同的目录中。它正在寻找tasks/build/ClickTask.java,但您的源是tasks/src/ClickTask.java。

第二种情况之所以有效,是因为它找到了源文件,但随后期望类文件最终出现在tasks/src中,但它并没有因此一直重建。

对于如何使用 Makefile 执行此操作,我没有一个好的答案,我倾向于将类文件放在同一目录中。或者,考虑使用 Ant 或 Maven,它们更容易支持这一点。

编辑:我认为这应该告诉你你需要什么:http://www.makelinux.net/make3/make3-CHP-8-SECT-1.html

I think this is because your src is in a different directory. It is looking for tasks/build/ClickTask.java but your source is tasks/src/ClickTask.java.

The second case works because it finds the source file, but then expects the class file to end up in tasks/src, which it doesn't hence it rebuilds all the time.

I don't have a good answer for how to do this with Makefiles, I tend to just put class files in the same directory. Alternatively, think about using Ant or Maven which supports this much more easily.

EDIT: I think this should tell you what you need: http://www.makelinux.net/make3/make3-CHP-8-SECT-1.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文