| CSC 161 | Grinnell College | Spring, 2012 |
| Imperative Problem Solving and Data Structures | ||
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.
Structs and typedefs in C.
Many ideas that follow come from exercises by Marge Coahran.
Be sure you understand the reading on headers and Makefiles for this lab.
The work that follows utilizes the scheme-like lists program from the recent lab.
For safety, make a new directory for multi-file scheme-lists and copy your scheme-lists program to it.
Separate the code in your scheme-lists program into three files as follows:
In order to get these source files to compile, both scheme-lists.c and scheme-lists-test.c should contain the following line at the top of the file:
#include "scheme-lists.h"
You should not #include your source file scheme-lists.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 type definitions in the file.
#ifndef _LISTINFO_H
#define _LISTINFO_H
...
#endif
Try building your program with the following command, typed at the shell prompt. Do you understand why it gives the error it does?
gcc scheme-lists-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 -c scheme-lists.c gcc -Wall -c scheme-lists-test.c
Use the shell command "ls" to check that the object files were produced.
Now create an executable file with the following command:
gcc -o scheme-lists-test scheme-lists-test.o scheme-lists.o
Check that the file scheme-lists-test exists with "ls -l", and the run the program with: "./scheme-lists-test"
Finally, compile both source files and link the resulting object files together with the following single command.
gcc -o scheme-lists-test scheme-lists-test.c scheme-lists.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 scheme-lists 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 scheme-lists-test.c. Doing so requires only the following simple steps:
The reading for this lab describes a detailed Makefile with directions for compiling, linking, and cleaning files related to a namelist.c program. This part considers a simplied Makefile.
Copy a simplified Makefile to your account and review it. Do you understand what is accomplished by each line in the file?
Adapt the Makefile so that it can be used to build your scheme-lists-test program. (You will, of course, want to change the "Author" comment at the top of the file when you do this.)
In the terminal window, type these commands:
Make a change in the source file scheme-lists.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.
Make a change in the header file scheme-lists.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.
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.
Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.