Simple make and (Makefile)

Recall, that to compile and run your p1 program from the command-line, you had to type each of the following commands.

    javac *.java
    java TestPQ NoPQ PQ01 PQ02 PQ03 MyPQ

To run the program and redirect the output to a file, you typed:

    java TestPQ NoPQ PQ01 PQ02 PQ03 MyPQ > results.txt

Wouldn't it be nice to save these commands somewhere so that you didn't have to retype them all the time? Yes, of course. And that is where Linux's make utility excels. The Linux make program provides way to improve your command-line program development process. make is a Linux program that interprets and executes the rules that are found in a Makefile

The basic process is:

  1. Create a text-only file named Makefile in your program directory.
  2. Add rules according the syntax required by the make program.
  3. Execute make to run the first rule in the Makefile.

1. Create a Makefile

Create a text-only file named Makefile where the name is case-sensitive and does not have a file extension.

2. Add Makefile rules

  1. rule_name :
    Place rule name at the start of a line and followed be a colon (:) and a new line.
  2. add commands, in order to execute. For each command:

3. Save the Makefile

Example: Makefile

make:
	javac *.java
	java MyJavaProgram

clean:
	rm MyProgram.class

CS400 p1's Makefile: Makefile

4. Run make

To run the code that is in the first Makefile rule, type:

   make

To run the code listed under the Makefile's clean rule, type:

   make clean

Wait! There's more ...

The make utility is much more powerful than this example. A web search for how to write a Makefile will provide dozens of examples and additional instructions. There are also other popular build utilities that can be run from a Linux command line, including Ant, Maven, and Gradle