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