Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile - applying a variable as a prefix to multiple parts of another variable

Tags:

makefile

javac

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.

like image 217
tth Avatar asked Mar 13 '11 11:03

tth


People also ask

How do you enter a prefix in makefile?

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.

What is $@ in makefile?

$@ 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.

What does += mean in makefile?

+= 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.

What does $( make mean in makefile?

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.


1 Answers

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’.

like image 76
Chen Levy Avatar answered Oct 07 '22 22:10

Chen Levy