Main Content

Makefile - some basics, and a demonstration

Archive - Originally posted on "The Horse's Mouth" - 2012-03-13 23:59:43 - Graham Ellis

Here is a basic rule from a makefile:

  mytarget:   mysource
      echo "myactions"
      uptime > mytarget
      cat -n mysource >> mytarget


mytarget depends on mysource. In other words, the following commands, which are the "rule" that's used to make mytarget from mysource are run if
1. mytarget does not exist or
2. mysource has a more recent timestamp than mytarget, indication that mytarget is out of date.

It is CRITICALLY IMPORTANT that each rule line is preceeded by tabs and not space.

I've created a file called mysource, but no file called mytarget. Let's see how that works:

wizard:mc graham$ ls -l my*
-rw-r--r-- 1 graham staff 45 13 Mar 13:06 mysource
wizard:mc graham$ make
echo "myactions"
myactions
uptime > mytarget
cat -n mysource >> mytarget


The actions (a.k.a. rules, a.k.a. script) have been run and mytarget created:

wizard:mc graham$ ls -l my*
-rw-r--r-- 1 graham staff 45 13 Mar 13:06 mysource
-rw-r--r-- 1 graham staff 122 13 Mar 13:20 mytarget


I run the make again, but there's nothing to be done as mytarget is up to date:

wizard:mc graham$ make
make: `mytarget' is up to date.
wizard:mc graham$ ls -l my*
-rw-r--r-- 1 graham staff 45 13 Mar 13:06 mysource
-rw-r--r-- 1 graham staff 122 13 Mar 13:20 mytarget


I modify mysource - the incoming file. That means mytarget is old (out of date) so when I rerun make, mytarget gets rebuilt:

wizard:mc graham$ echo "Hello World again" >> mysource
wizard:mc graham$ ls -l my*
-rw-r--r-- 1 graham staff 63 13 Mar 13:22 mysource
-rw-r--r-- 1 graham staff 122 13 Mar 13:20 mytarget
wizard:mc graham$ make
echo "myactions"
myactions
uptime > mytarget
cat -n mysource >> mytarget
wizard:mc graham$ ls -l my*
-rw-r--r-- 1 graham staff 63 13 Mar 13:22 mysource
-rw-r--r-- 1 graham staff 147 13 Mar 13:22 mytarget


But if I repeat the same make, mytarget is now correctly dated and so there is nothing to do:

wizard:mc graham$ make
make: `mytarget' is up to date.
wizard:mc graham$