by Henry
2018-09-06
"Make files" reduce the amount of typing required to compile code in your command-line interface. This guide will take you through writing a make file and using it, then explain what you just did. In this guide, C is first, then Java.
Make comes installed on most Linux and OS X machines. For Windows machines, download and install GnuWin32. As for compiling, as long as you have a JDK, the Java part of this guide should work fine. However for C, Windows users can download MinGW and swap out the use of .out's for .exe's in this guide. Remember to add any new tools to your Path environment variable.
#include
int main () {
printf("asdf\n");
}
run : example.out
./example.out
example.out : test.c
gcc test.c -o example.out
This is an example of using two types of lines in Make. The first type defines a command that you would like to use and is followed by a tab-indented set of real commands that that new command will run. It looks like this:
command name : required files
commands to execute
Example:
run : example.out
./example.out
As you can see, the next time you want to run a.out, you can just enter make run.
However, the above assumes you already have a compiled executable. A second type of line is needed to compile source files:
required executable : source file
commands to execute
Example:
example.out : test.c
gcc test.c -o example.out
This file allows you to use make run when in the program directory. Upon which, the program will recompile if needed, and then execute. This is an alternative to typing:
gcc test.c -o example.out
./example.out
every time you want to compile and run the program.
default : example.out
./example.out
a.out : test.c
gcc test.c -o example.out
This is the same as the above only now, make will compile and run the program.
For java, things are pretty similar.
default : MyClass.class dependency.class
java MyClass
%.class : %.java
javac $<
clean :
rm *.class
The %.class : %.java is a catch-all for the required files, taking care of both MyClass.class and dependency.class or any *.class file needed by the default command. The $< is one of make's automatic variables. In this case, $< takes the name of whatever .java file being processed at the time. This is an alternative to making separate lines for each .java file.
The clean command at the end is an example of an extra command that may be convenient to use. In this case it removes .class files from the directory.
Make can decide whether or not to recompile a program based on whether changes have occured since the last compilation. However, it will only check for changes in files listed as targets. In the above example, we include dependency.class in the list of targets because we want it to detect changes to its source file. If we did not include it, it would still work but it would use the old version without recompiling. For more information on makefiles see https://www.gnu.org/software/make/manual/make.html
Windows version of C Makefile using MinGW gcc compiler:
default : a.exe
a.exe
a.exe: test.c
gcc test.c