Originally Published: Thursday, 23 March 2000 Author: Derek Barber
Published to: develop_articles_tutorials/Development Tutorials Page: 1/1 - [Printable]

Getting Started with Make - Part 1: The Basics

For many aspiring Linux programmers, especially those familiar with the integrated development environments of the Windows world, learning how to use the make utility is an essential addition to their repertoire.

   Page 1 of 1  

Getting Started with Make Series
1. The Basics
2. Makefiles, Variable Definitions, and Rules
3. Directives

For many aspiring Linux programmers, especially those familiar with the integrated development environments of the Windows world, learning how to use the make utility is an essential addition to their repertoire.

What is make? To put it simply, make is a utility that keeps track of how to compile and link your program. To use make, you need to create a Makefile, which is a text file that tells make what instructions to use in compiling and linking your program. Make also tracks dependencies between your source files, so when you change any of the files, only the necessary files are recompiled. This is a time-saving feature that becomes especially apparent for large projects. Over the next few weeks we will go over all the standard uses of make, including some advanced features. Today we'll start with the basics.

So, exactly how do you get started with make? Let's start with a simple example and then explain all the pieces. Say you have the following two C++ source code files:

main.cc:

  #include <iostream.h>
  #include "hack.h"

  int main(void)
  {
    hack();
    return(0);
  }

hack.cc:

  #include <iostream.h>

  void hack(void)
  {
    cout << "I love to hack!" << endl;
  }

Don't forget about the header file for hack.cc:

hack.h:

  void hack(void);

Now, normally on the command line you might use the following command to compile your program.

  /home/dbarber$ g++ -o main main.cc hack.cc

To run the program, you would type:

  /home/dbarber$ ./main
  I love to hack!

Okay, now it may not seem like a Makefile would be useful in this case, and in reality it may not be. However, for demonstration purposes, let's create a Makefile for this program.

Makefile:

  main : main.o hack.o
    g++ -o main main.o hack.o

  main.o : main.cc
    g++ -c main.cc

  hack.o : hack.cc
    g++ -c hack.cc

  clean :
    rm main main.o hack.o

Now, all you have to do to compile the program is type:

    /home/dbarber$ make

So, exactly what is going on inside this makefile? Let examine it closely.

Each makefile consists of several rules which are the fundamental building blocks of makefiles. The purpose of the rule is to outline exactly how to make a program. The above makefile happens to contain three rules, one of the rules being:

  main : main.o hack.o
    g++ -o main main.o hack.o

Each rule has several parts: a target list, a dependency list, and a command list. In this case, the target is main, which happens to be the name of the executable to create. The dependency list consists of main.o and hack.o, and are the necessary files that are required for creating the target. Not every rule will have a dependency list; an example of this is the clean rule above. The command list is just one command, g++ -o main main.o hack.o. The command part can be more than one command, but you must remember to always indent the command list with the Tab character.

So, due to the dependency checking of make, if you happened to modify a source file, only the necessary files would be recompiled. For our simple program, this may not seem to be that great. However, I'm sure any member of a large programming project such as gcc would realize the significance of that feature.

Well, I hope that this issue covers the basics of make and helps you to start creating your own basic makefiles. In the next installment we'll delve deeper into the workings of the make utility.

Derek Barber (dbarber@apiva.com) is a project manager for Apiva.com, a company developing search engine technology and Linux-based software.





   Page 1 of 1