How to write multilevel-recursive make file to build java files in sub-directory structure

This is a quick post showing how to write makefile which can build files recursively in sub-directories . When i wanted this info for one of my academic projects, i could not find a quick one online and had to refer to docs . So here it is….

Let me first describe the file structure. I have a parent folder called  ”dos” which consists of  2 directories ; server, client & one make file as illustrated in image below.

These sub directories contain java source code which needs to be compiled on hitting make.

The directory structure.

Recursive Make file

The idea is to have individual make files in each sub-directory which needs to be built and calling these make files from a main make file in the parent directory.

The content of  make file in parent directory is shown below. Here “server” and “client” are the sub-directories which contain the java files to be built.

.SUFFIXES: .java .class

.java.class:
	javac $<

CLASSES:
	cd server && $(MAKE)
	cd client && $(MAKE)

all: $(CLASSES) 

clean:
	cd server && rm -r *.class
	cd client && rm -r *.class

The content of make file in sub-directory is a simple make file which lists all files to be built.

 

.SUFFIXES: .java .class

.java.class:
	javac $<

CLASSES = client.class ClientThread.class Message.class Server.class

all: $(CLASSES)

clean:
	rm -f *.class

To build these files run make from parent directory. Don’t forget to add individual make files in sub-directory.



											
This entry was posted in Programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>