CSC 161 Grinnell College Spring, 2009
 
Imperative Problem Solving and Data Structures
 

Program Management: Header Files and make

Goals:

This lab provides practice dividing a program into pieces, compiling those pieces separately, linking components together to form an executable, and automating this process using the Unix utilities make and Makefile.

Prerequisites

Structs and typedefs in C.

Acknowledgement

Many ideas that follow come from exercises by Marge Coahran.

Index

  1. Header and Implementation Files
  2. Makefiles

Steps for this Lab

  1. Be sure you understand the reading on headers and Makefiles for this lab.

Part A: Header and Implementation Files

The work that follows utilizes the array implementation of queues from a recent lab.

  1. For safety, make a new directory for multi-file queues and copy your queue-array program to it.

  2. Separate the code in your queue-array program into three files as follows:

    In order to get these source files to compile, both queue-proc.c and queue-test.c should contain the following line at the top of the file:

       #include "queue.h"
    

    You should not #include your source file queue-proc.c anywhere. (It is good practice to only include header files, and not implementation files, in other source files.)

    As discussed in the reading, your header file should contain the following lines, and they should surround all other code in the file (i.e., all other code goes where the ellipses are shown).

       #ifndef _QUEUEINFO_H
       #define _QUEUEINFO_H
    
         ...
    
       #endif
    
  3. Try building your program with the following command, typed at the shell prompt. Do you understand why it gives the error it does?

       gcc -ansi queue-test.c
    

    Compile each of your source files (to create object files, but still not an executable program) with the following commands, typed at the shell prompt:

       
       gcc -Wall -ansi -c queue-proc.c
       gcc -Wall -ansi -c queue-test.c
    

    Use the shell command "ls -l" to check that the object files were produced.

    Now create an executable file with the following command:

       gcc -o queue-test queue-test.o queue-proc.o
    

    Check that the file queue-test exists with "ls -l", and the run the program with: "./queue-test"

    Finally, compile both source files and link the resulting object files together with the following single command.

       gcc -o queue-test queue-test.c queue-proc.c
    

    Note, however, that building large programs this way quickly becomes tedious since every source file must be recompiled from scratch.

As you finish this part of the lab, note that you have converted your queueString code into an abstract data type. By listing the function prototypes, the header file tells other programmers what operations are supported by the data type. It does not specify how they are implemented; that is done in the implementation file.

Your data type may now be used in client programs, such as queue-test.c. Doing so requires only the following simple steps:

Part B 2: Makefiles

  1. Copy ~walker/c/lists/prog-mgmt/Makefile to your account and review it. Do you understand what is accomplished by each line in the file?

  2. Adapt the Makefile so that it can be used to build your queue-test program. (You will, of course, want to change the "Author" comment at the top of the file when you do this.)

  3. In the terminal window, type these commands:

  4. Make a change in the source file queue-proc.c. For example, you could add or modify a printf statement in some function. Which rules in the makefile do you expect will be run, the next time you invoke make, as a result of this change?

    Run make again, to verify your prediction experimentally.

  5. Make a change in the header file queue.h. For example, you could add a comment to the top of the file. Which rules in the makefile do you expect will be run, the next time you invoke make, as a result of this change?

    Run make again, to verify your prediction experimentally.

  6. If you still have time, take a look at GNU's documentation regarding make and makefiles here: Makefiles and the GNU make utility.

    Note that this documentation is very lengthy, and it is not necessary for you to read or understand all of it in order to begin using makefiles. Reading through Section 3.2 or so provides a good introduction to the topic.


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.sp09/labs/lab-prog-mgmt.shtml

created 29 March 2007 by Marge Coahran
revised 6 April 2008 by Marge Coahran
revised 14 April 2008 by Henry M. Walker
last revised 25 January 2009 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.