Make
GNU Make (https://www.gnu.org/software/make/) is a tool which controls the generation of executables and other non-source files of a program from the program's source files. It was created to manage compiling C/C++ code in the 70s but these days it can be used universally for any language.
Make is configured for a project via a Makefile
that is like a recipe for building a project.
An Introduction to Makefiles
You need a file called a Makefile to tell make what to do. The Makefile acts as a recipe telling make exact steps in how to compile and link a program. Make gets its knowledge of how to build your program from the Makefile, which lists each of the non-source files and how to compute it from other files.
Format
Suggested reading: Chapter 3 of the GNU Make Manual
Writing Build Rules
Suggested reading: Chapter 4 of the GNU Make Manual
Example
Makefile for Java programs: https://github.com/bc-cisc3140-mw2-fall2019/cisc3140/tree/master/make/java_example
# makefile begins
# comments start with #
JC = javac
JVM = java
.SUFFIXES: .java .class
# Here is our target entry for creating .class files from .java files
# This is a target entry that uses the suffix rule syntax:
.java.class:
$(JC) $*.java
# MAIN is a variable with the name of the file containing the main method
MAIN = Main
# CLASSES is a macro consisting of N words (one for each java source file)
# When a single line is too long, use \<return> to split lines that then will be
# considered as a single line.
CLASSES = \
Foo.java \
$(MAIN).java
# the default make target entry
default: classes
$(JVM) Main
# Below we are replacing the suffix .java of all words in the macro CLASSES with the .class suffix
# This target entry uses Suffix Replacement within a macro:
# $(name:string1=string2)
# In the words in the macro named 'name' replace 'string1' with 'string2'
classes: $(CLASSES:.java=.class)
# Next two lines contain a target for running the program
# Remember the tab in the second line.
# $(JMV) and $(MAIN) are replaced by their values
run: $(MAIN).class
$(JVM) run
public class Main {
public static void main (String[] args) {
System.out.println("Hello World");
}
public void MyMethod() {
System.out.println("Printing from Super Class");
}
}
References:
- GNU Make Documentation https://www.gnu.org/software/make/
- Learn X in Y minutes tutorial https://learnxinyminutes.com/docs/make/
- Managing Projects with Gnu Make http://uploads.mitechie.com/books/ManagingProjectswithGNUMakeThirdEdition.pdf
- Examples: https://makefiletutorial.com/