Make your own free website on Tripod.com
GNU make

GNU make


This is the method for making an executable file from non-source file/s of a program. The important keywords that we must bear in mind are: target, source (what we require) and action. It's very simple, actually. It is like when you want to cook, you will decide the menu, what ingredients do you need and actions you will take to prepare your delicious food. So, the steps for using 'make' are also as simple as that. Here, I will explain how to use 'make' in a simpler manner.


Step 1: Create any *.c file/s using VI editor.

You can try the given program if you want to. Here, I will give an example which involving subroutine (aka call function). Because, in real program, we always face a problem of having very lengthy and disorder sourcecode. So, by using subroutine, it will make your life much more easier. Trust me!!

emelia@ffv3vf:~$vi script.c

#include <stdio.h>

void function(void);

int main()
{
   printf("\n Hello.");
   printf("\n How are you?");
   
   function();                // call function
}

Then, create other *.c file to store your subroutine.

emelia@ffv3vf:~$vi call.c

#include <stdio.h>

void function(void);
{
   printf("\n Good Luck and Have Fun"); 
}

Therefore, you will have 2 new files; script.c and call.c

Step 2: Create Makefile file using VI editor.

When you write a program, you will need a Makefile for it, so that, it is possible to use 'make' to build and install the program.This Makefile will lists all the files and how to compute it from other files. Then, 'make' will get its knowledge of how to build your program from this Makefile. The basic syntax of Makefile can be illustrated as follows:

 target : requirement 
 	action

target : You must decide the final output you want. This output is in binary form.

requirement : The source file (*.c or *.o file/s) that you need to produce your output .

action : this is the command you frequently type at the console when you want to execute a program.

So, our Makefile will look like this:

emelia@ffv3vf:~$vi Makefile

script : script.o call.o
        gcc -o script script.o call.o
script.o : script.c
        gcc -c script.c
call.o : call.c
        gcc -c call.c
clean :
        rm script 
        rm *.o


Flow of execution of files are:
    Flow: script.c file (source code) --> script.o file (object file) --> script* (binary file)

Command: gcc -c script.c gcc -o script script.o call.o gcc -c call.c

Step 3: Execute the program

First of all, list the files in your directory. Later, you will see the differences.

emelia@ffv3vf:~$ ls -l
total 12
-rw-r--r--  1 emelia users 187 2006-07-04 11:32 Makefile
-rw-r--r--  1 emelia users  86 2006-07-04 11:31 call.c
-rw-r--r--  1 emelia users 161 2006-07-04 11:24 script.c

Then, to compile, just type 'make' at the console.
emelia@ffv3vf:~$ make
gcc -c script.c
gcc -c call.c
gcc -o script script.o call.o

The commands shown at the console are the command that have been executed using 'make' command.

Then, list again all the files.

emelia@ffv3vf:~$ ls -l
total 32
-rw-r--r--  1 emelia users   152 2006-07-04 11:36 Makefile
-rw-r--r--  1 emelia users    91 2006-07-04 11:39 call.c
-rw-r--r--  1 emelia users   844 2006-07-04 11:40 call.o
-rwxr-xr-x  1 emelia users 11191 2006-07-04 11:40 script*
-rw-r--r--  1 emelia users   161 2006-07-04 11:37 script.c
-rw-r--r--  1 emelia users   920 2006-07-04 11:40 script.o

You can see the differences, right? There are additional files; call.o, script.o and script* in the lists. The file named script* is a binary file and will be used upon execution.

Finally, the output of the file is:
emelia@ffv3vf:~$./script

 Hello.
 How are you?
 Good Luck and Have Fun!!

Remember that we also define 'clean' command in our Makefile. So, when we want to remove any file, we can simply do so by using 'make clean' command.

emelia@ffv3vf:~$ make clean
rm script
rm *.o

emelia@ffv3vf:~$ ls -l
total 12
-rw-r--r--  1 emelia users 152 2006-07-04 11:36 Makefile
-rw-r--r--  1 emelia users  91 2006-07-04 11:39 call.c
-rw-r--r--  1 emelia users 161 2006-07-04 11:37 script.c

There you go! No more object and binary files in our list. If you want to execute the program again, just 'make' and repeat the same step.

Try This Out and Good Luck!!

Return to Linux home page