Book Cover

Introduction to Scientific Programming
Computational Problem Solving Using:
Maple and C
Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Separate Compilation Tutorial

All of the programs that we have looked at to this point have been contained within a single file. This is the exception rather than the rule. As programs become larger, it is important to spread them across files of a reasonable size. In this lesson we will look at why this is important, and how it can be done.

Compiling Files

Up to this point, whenever you wanted to compile a program you compiled the file displayed in an Emacs buffer. The Compile This File... option of Emacs' Compile menu (or equivalently, M-x compile) is a handy feature when your entire program is contained with one file. It is, of course, possible to compile a file completely independently of Emacs.

Download hello.c. It is a simple program that prints out ``Hello world'', and you can compile and run it without ever displaying it in Emacs. To compile it, go to a Unix Shell window, connect to the directory into which you downloaded the program, and enter the following command:

gcc -Wall -g -c hello.c

This command will result in a new file being created. Use the ls command to list your directory. What file is there that wasn't before?

Click here for the answer

At this point you still don't have an executable program that you can run. The next step is to link the object file to produce such an executable. To do this, type the following into your UNIX Shell window:

gcc -o hello hello.o -lm

Linking hooks up your compiled code with some additional C-specific code. Once again, a new file has been created in your directory. List the directory. What new file do you find?

Click here for the answer

Now that you have an executable you can run it in the familiar way. Simply type ``hello'' into the UNIX Shell window.

To summarize, the steps involved in compiling, linking, and running a program are:

  1. Compile the ``.c'' file containing the source code with a command such as

    gcc -Wall -g -c hello.c

    The -c in the command is important--it tells C to produce an object file called (in this case) ``hello.o''. The -Wall part is optional but is strongly encouraged--it tells C to produce all possible warning messages. A warning message will usually lead you to a bug in your program. The -g tells the compiler to include information needed by the debugger.

  2. Link the ``.o'' file to produce an executable with a command such as

    gcc -o hello hello.o -lm

    The -o hello in the command is important--it tells C what to name the executable. The -lm part tells C to link in the math libraries. If your program doesn't use any of the match functions from ``math.h'', you can leave this part out.

  3. Run the executable in the usual way.

    If your entire program is contained within a single file, you can do the compilation and linking in one step by entering the command

    gcc -Wall -g -o hello hello.c -lm

This is exactly what Emacs does when it compiles a program for you. We have been careful to this point to do the compilation in two separate steps so that you will be able to deal with programs made up of more than one file. We will explore this topic further in the next section.

Multiple Files

Consider the idea of dividing programs into more than one file. By that we mean putting the various functions that make up the program into more than one file.

Try to think of some reasons why this might be a good idea.

Click here for some possible answers

Now let's take a look at a program divided into more than one file. Download main.c and help.c and view them in Emacs. Notice that main.c makes use of the two functions defined in ``help.c''.

Is there any way that you can compile this program from Emacs? Try compiling ``main.c'' this way. What happens?

Click here for the answer

Now try compiling the buffer containing ``help.c''. What happens?

Click here for the answer

Compiling Multiple Files

Based upon what you already know, what do you suppose would be the procedure for compiling, linking, and running the program that we've spread across two files here? Think about it carefully before you look at the answer.

Click here for the answer

Now do it yourself. Compile, link, and run the program.

When you've succeeded in doing that, try the following experiment. Modify the main program slightly to call the two functions in the opposite order. What do you think will be involved in recompiling, linking, and running the new version of the program?

Click here for the answer

Compile, link, and run your modified program. (Don't forget to save ``main.c'' before you do so!)

Declaring Functions

There's still a problem remaining. Whenever you compile ``main.c'', the compiler complains that ``sum'' and ``product'' are undeclared functions. Even so, the linker succeeds in putting together an executable. Why should we worry about the compiler warning if we can get an executable program anyway?

Click here for an answer

It is actually not too difficult to fix up this problem. Simply add the following two lines to ``main.c'' up above the main procedure.

int sum (int n);
int product (int n);

Where have you seen these lines before?

Click here for the answer

Save ``main.c'' and rebuild the executable. If you've done everything correctly, the errors should go away.

Include Files

This is a clumsy way to go about declaring functions that are implemented in a separate file. Can you think of some problems?

Click here for an answer

Here's how we can deal with both of these problems. Create a new file that contains nothing but the declarations of the functions in ``help.c''. In fact, we have already created just such a file: download help.h and take a look at it. What is in there in addition to the declarations?

Click here for the answer

Now, modify ``main.c'' by eliminating the declarations you added, and replacing them with the line

#include "help.h"

As you probably recall, this is a direction to the compiler to behave as if the contents of the file ``help.h'' were actually typed in here. Notice that we have surrounded the name of the file with quotes instead of angle brackets as is the case with, for example, stdio.h. Why?

Click here for the answer

Save ``main.c'' and compile, link, and run it. How does this address problem (1) identified above?

Click here for the answer

However, we still haven't addressed issue (2). There is nothing to stop us from modifying the declarations in ``help.h'', rendering it inconsistent with ``help.c''.

Fortunately, there's a way to deal with this problem too. Put the line

#include "help.h"

at the top of ``help.c''. Now save ``help.c'' and rebuild the executable. You should encounter no errors.

Now edit ``help.c'' again by changing one of the declarations. For example, change ``sum'' so that it takes a float as an argument instead of an int. Save and recompile ``help.c''. What happens?

Click here for the answer

Summary

To summarize, here is the suggested procedure for dealing with multiple-file programs in C.

  1. For each file ``file.c'' containing functions that will be used elsewhere, create a file ``file.h'' containing the header information and comments for those functions.
  2. Put the line

    #include "file.h"

    near the beginning of ``file.c''. This way, the compiler will warn you if the two sets of declarations ever become different.

  3. Put the line

    #include "file.h"

    near the beginning of all files that use the functions in ``file.c''. This way, the complier will warn you if you use the functions incorrectly.

You can then separately compile the ``.c'' files, and then link together to create an executable. In the next lesson we will look at Makefiles, which provide a more convenient way of compiling and linking multiple file programs.