7

i have a small java project i want to build using a makefile, the code is in src/package... /*.java, the bytecode should go to bin/package.../*.class.
My current file looks like this (simplified):

JC = javac
SRCDIR = src
BINDIR = bin
JCFLAGS = -d $(BINDIR)/

CLASSES = $(SRCDIR)/package/class1.java $(SRCDIR)/package/class2.java $(SRCDIR)/package/class3.java 

default:
    $(JC) $(JCFLAGS) $(CLASSES)

It works and does what it should, but there has to be a more elegant way to do this.
For example, is there a way to apply the path ($(SRCDIR) and the package name) as a prefix to all class filenames, so i do not have to put the path seperately in front of every class?

All classes have to be compiled in one javac-call, as there are circular dependencies in them, so using an own target for each class does not work:

default: $(CLASSES)
%.java:
    $(JC) $(JCFLAGS) $(SRCDIR)/$@

Thanks for your help.

tth
  • 73
  • 1
  • 3

1 Answers1

12

From the GNU make manual:

$(addprefix prefix,names...)

The argument names is regarded as a series of names, separated by whitespace; prefix is used as a unit. The value of prefix is prepended to the front of each individual name and the resulting larger names are concatenated with single spaces between them. For example,

$(addprefix src/,foo bar)

produces the result ‘src/foo src/bar’.

Chen Levy
  • 15,438
  • 17
  • 74
  • 92