Sunspawn
Sunspawn

Reputation: 837

Yet another "multiple target patterns" makefile error

I tried looking around, but all of the questions I saw here were too high-level for me to understand.

Here's my makefile:

compile: bin src cmp
bin: mkdir bin
src: find src -name "*.java" > sources.txt
cmp: javac -cp biuoop-1.4.jar -d bin @sources.txt
run: java -cp biuoop-1.4.jar:bin:src/resources Ass6Game
jar: jar -cmf Ass6Game.jar Manifest.txt -C bin . -C src resources

When I try to run make compile, I get the "multiple target patterns" error. What did I do wrong?

Upvotes: 1

Views: 2800

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

Your makefile syntax is incorrect.

The syntax of a make rule is

In general, a rule looks like this:

targets : prerequisites
   recipe
   …

or like this:

targets : prerequisites ; recipe
   recipe

Whereas you have your recipe lines in the prerequisite location and so the : in the java command is confusing make.

Your makefile should look like this

compile: bin src cmp
bin: ; mkdir bin
src: ; find src -name "*.java" > sources.txt
cmp: ; javac -cp biuoop-1.4.jar -d bin @sources.txt
run: ; java -cp biuoop-1.4.jar:bin:src/resources Ass6Game
jar: ; jar -cmf Ass6Game.jar Manifest.txt -C bin . -C src resources

or like this

compile: bin src cmp
bin:
        mkdir bin
src:
        find src -name "*.java" > sources.txt
cmp: 
        javac -cp biuoop-1.4.jar -d bin @sources.txt
run:
        java -cp biuoop-1.4.jar:bin:src/resources Ass6Game
jar:
        jar -cmf Ass6Game.jar Manifest.txt -C bin . -C src resources

or to actually use what make provides for you a bit more something more like this

JAR := biuoop-1.4.jar
SOURCES := $(shell find src -name '*.java')
# Or if src is a single directory without sub-directories
#SOURCES := $(wildcard src/*.java)

GAMEJAR := Ass6Game.jar

.PHONY: all
all: $(JAR)

bin:
        mkdir bin

$(JAR): $(SOURCES) | bin
        javac -cp $@ -d bin $^

# See http://www.gnu.org/software/make/manual/make.html#Force-Targets for what `FORCE` is doing here.
$(GAMEJAR): FORCE
        jar -cmf $@ Manifest.txt -C bin . -C src resources

FORCE: ;

.PHONY: run
run: $(GAMEJAR)
        java -cp $(JAR):bin:src/resources $(GAMEJAR)

which follows the Rules of Makefiles a bit better and actually lets make intelligently rebuild your files as necessary.

Upvotes: 1

Related Questions