Next: , Previous: BP Compatible Compiler Directives, Up: Borland Pascal

7.6 Units, GPI files and Automake

You can use units in the same way as in Borland Pascal. However, there are some additional features.

Concerning the syntax of a unit, you can, if you want, use Extended Pascal syntax to specify a unit initializer, i.e., instead of writing


at the end of the unit, you can get the same result with

     to begin do

and there also exists

     to end do

which specifies a finalization routine. You can use this instead of Borland Pascal's exit procedures, but for compatibility, the included System unit also provides the ExitProc variable. The to begin do and/or to end do parts must be followed by the final end.. See Modules, for information about Extended Pascal modules, an alternative to units.

When GPC compiles a unit, it produces two files: an .o object file (compatible with other GNU compilers such as GNU C) plus a .gpi file which describes the interface.

If you are interested in the internal format of GPI file, see GPI files.

If you want to compile a program that uses units, you must “make” the project. (This is the command-line switch -M or the IDE keystroke F9 in BP.) For this purpose, GPC provides the command-line switch --automake:

     gpc --automake hello.pas

If you want to force everything to be rebuilt rather than only recompile changed files (-B or “build” in BP), use --autobuild instead of --automake:

     gpc --autobuild hello.pas

For more information about the automake mechanism, see Automake.

If you do not want to use the automake mechanism for whatever reason, you can also compile every unit manually and then link everything together.

GPC does not automatically recognize that something is a unit and cannot be linked; you have to tell this by a command line switch:

     -c            only compile, don't link.

(If you omit this switch when compiling a unit, you only get a linker error message undefined reference to `main'. Nothing serious.)

For example, to compile two units, use:

     gpc -c myunit1.pas myunit2.pas

When you have compiled all units, you can compile a program that uses them without using --automake:

     gpc hello.pas

However, using --automake is recommended, since it will recompile units that were modified.

You could also specify the program and the units in one command line:

     gpc hello.pas myunit1.pas myunit2.pas

One of the purposes of writing units is to compile them separately. However, GNU Pascal allows you to have one or more units in the same source file (producing only one .o file but separate .gpi files). You even can have a program and one or more units in one source file; in this case, no .o file is produced at all.