make or how to create a makefile

make tool or make file is a simple rule based system to help developers/admins to do repetitive jobs. Here is a basic format of how a make file looks like.

How does a make_file syntax looks like?

# syntax of makefile

<rule02>:<track_file1> <track_file2>

<rule03>:<track_file1> <track_file2>

How do you run a make command?

bash-3.2$ date
Tue Mar 22 21:56:16 IST 2015
bash-3.2$  make rule01

Few rules for using this rule based system:

  • File name can be Makefile or makefile
  • File should in the location where you want to execute make commands
  • with no arguments, make executes the first rule in the file.
  • By putting the list of files on which the command depends on the first line after the “:”, make knows that rule needs to be executed if any of those files change.(Like a Trigger)
  • use only tabs for formatting code.
  • make expects executable code is in one line but for convenience, you can use \ for increasing its scope.

Here is a crazy example/sample makefile for some special people

SDS-bash3.2$ ls -l Makefile
-rw-r--r--  1 sampathm  wheel  253 Mar 29 11:36 Makefile
SDS-bash3.2$ cat Makefile
#! /usr/bin/make

# Aliases
GREETINGS="hello sam"

# Helper function - default
    echo "InputError:\
    Try: make list"

# list of commands
    echo "make game # does the stuff"\

# command 1
    echo ${GREETINGS}

# command n
    rm -f *.o
SDS-bash3.2$ make clean
rm -f *.o
SDS-bash3.2$ make # to see default working
echo "InputError:\
    Try: make list"
InputError:Try: make list
SDS-bash3.2$ make list
echo "make game # does the stuff"\

make game # does the stuff
SDS-bash3.2$ make clean
rm -f *.o

You can consider that this is an introduction to start for learning make. Here is another short quick tutorial for make (, where author mention about how to use variable & regular expression and other operations in make_file.