CIS 22 - Data Structures: Separate Compilation


Header Files

Whenever you create a source file (suppose it is called func.c) containing one or more functions, you should create an associated .h file, called the header file. (In this case the header file would be called fun.h.) The header file should contain the function prototypes, typedefs, and possibly global variables (which are shown to be global by using the keyword extern.) The header file should not contain declarations of variables that are local to the functions in fun.c. The purpose of the header file is to communicate information needed by the calling routine, such as the function prototype.

When you code another source file which calls one of the routines in the fun.c file, that calling file must contain the line

	#include fun.h
NOTE:You do not include fun.c.

Separate Compilation

Suppose that you have a main program, prog.c, and two function files f1.c and . (Of course, you also have the associated header files f1.h and f2.h). You can compile files f1.c and f2.c first by using the commands
	acc -c f1.c
	acc -c f2.c
from the Unix prompt. If you are running under DOS, you can do the same from the command line, using bcc or tcc for Borland or Turbo C.

The -c option on the compilation causes the compiler to generate only an object file. Under Unix, the object files will be named f1.o and f2.o; under DOS they will be named f1.obj and f2.obj.

To compile the main program, you will use the command

	acc prog.c f1.o f2.o
under Unix, and similarly using bcc or tcc and .obj under DOS. This will generate an executable file, named a.out under Unix, or prog.exe under DOS.

You can more efficiently manage projects involving separate files by using the Unix makefile utility, or by using the project facility of the DOS/Windows-based compilers.

Redirecting input and output

Redirecting stdin and stdout under DOS has to be done from the command line, so the commands are similar to the Unix commands, except that the executable is called ....exe instead of a.out.

To redirect stdin only:

	a.out < inputfile
To redirect stdout only:
	a.out > outputfile
To redirect both stdin and stdout:
	a.out < inputfile > outputfile

Warning for those working under Windows

Apparently, if you compile under Windows, the code generated will not run from the DOS command line. You have to select the appropriate option to generate DOS code rather than the default Windows code.


Back to Managing Projects page
Back to CIS 22 Home Page