makefile for java
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. 开发者_如何学Gozsh: 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)
@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) $<
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.
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
精彩评论