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.
The prefix is want is /usr for most packages. So --prefix=/usr will do the job, but not all packages come with configure, a lot of them have just a makefile. How to set the prefix? I'm using dwm as an example and it uses /usr/local as prefix.
$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.
+= is used for appending more text to variables e.g. objects=main.o foo.o bar.o. objects+=new.o. which will set objects to 'main.o foo.o bar.o new.o' = is for recursively expanded variable.
And in your scenario, $MAKE is used in commands part (recipe) of makefile. It means whenever there is a change in dependency, make executes the command make --no-print-directory post-build in whichever directory you are on.
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’.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With