Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Pascal/HP-UX Programmer's Guide > Chapter 2 Program Structure

Separate Compilation

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

Separate compilation is the process of separating the source for a large program into pieces that can be compiled independently of other pieces.

There are several reasons why compiling pieces of a program separately is practical:

  • When the program is too long to compile.

  • When the program is too complex to manage.

  • When the program is being worked on by more than one programmer or by a team of programmers.

There are four methods used for separate compilation. They are performed by using modules and by using the compiler options SUBPROGRAM, GLOBAL, and EXTERNAL.

Using modules is the preferred method for separate compilation from a structured programming point of view. However, using modules does have certain limitations, as does using SUBPROGRAM, GLOBAL, and EXTERNAL. You must decide which method works in the way you prefer for your specific situation.

The remainder of this section addresses separate compilation using modules and each compiler option. The uses, advantages, and disadvantages of each method are discussed to help you determine which one to use.

For detailed information on SUBPROGRAM, GLOBAL, and EXTERNAL, refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual, depending on your implementation. For more information on modules, see “Using Modules ” in this chapter.

Using Modules

Once a module is created, the import statement makes that module available to any other program or module. The importing compilation unit receives the constant, type, variable, procedure, and function declarations that are exported by the imported module.

When to Use Modules

Use modules for separate compilation when you have an extremely large program, when you would like easy accessibility to libraries, or when you are building shared or executable (XLs) libraries.

Advantages of Using Modules

Some advantages to separate compilation with modules are:

  • Many modules can exist within an executable program, but with only one main program.

  • When a module changes, you only need to recompile units that refer to the module.

  • You can import types and variables from a module without distributing the source. For example, you can extract information from an object file, archive library, or MLIBRARY.

  • The types and the object code are in sync. There's no possibility of a mismatch.

  • The constant, type, variable, procedure, and function declarations that are not exported are hidden.

  • Modules can be placed in shared libraries or XLs.

Using SUBPROGRAM

The SUBPROGRAM compiler option turns a Pascal program into a subprogram compilation unit.

For separate compilation, SUBPROGRAM must be included in all compilation units, except the compilation unit containing the outer block. No code is generated for the outer block if used.

When to Use SUBPROGRAM

SUBPROGRAM is recommended for use in compilation units where the global variables won't change much.

SUBPROGRAM Advantages

Using SUBPROGRAM results in smaller object files and less link time. You also get faster access to the first 8K bytes of globals. The SUBPROGRAM option can also be specified with a list of routines to compile as few as one procedure, if RLFILE is used.

SUBPROGRAM Limitations

The variables must be in the exact same order and must be declared with the same types. Otherwise, at run time the global variables used in one compilation unit may not match the actual memory that matches the global variables in a different compilation unit.

To avoid this problem, place all global variable, type, and constant declarations in a file and include ($INCLUDE$) those files in all compilation units. If you do not ensure that the variable, type, and constant declarations match in all compilation units, your execution results will be incorrect, but no error will occur at compile time or at link time. Note that global variables, compiled with this option, can not be placed in shared libraries or XLs.

Using GLOBAL/EXTERNAL

The GLOBAL and EXTERNAL compiler options turn Pascal programs into global and external compilation units. The compiler options must precede the reserved word program.

The GLOBAL compiler option:

  • Generates symbolic definitions for the global variables in the compilation units.

  • Generates code for the outer block and any procedures.

The EXTERNAL compiler option:

  • Generates symbolic references for the global variables in the compilation unit.

  • Prevents the compiler from generating storage for global variables.

  • Does not generate code for the outer block and prevents the compiler from generating an outer block. If there are statements in an outer block, they are ignored.

When to Use GLOBAL/EXTERNAL

Use GLOBAL/EXTERNAL when sharing global information with another language, when the number of global variables are too large to recompile each time, and when building shared libraries or XLs.

GLOBAL/EXTERNAL is also useful when global variables will change often.

GLOBAL/EXTERNAL Advantages

The following are some advantages of using GLOBAL/EXTERNAL:

  • When you use GLOBAL/EXTERNAL for separate compilation, the global variables do not need to be listed in the same order.

  • Because the variables are matched by name, only as many globals as used need to be declared when using EXTERNAL.

  • The storage for globals does not take up space in the program file.

  • The global variables can be placed in shared libraries.

GLOBAL/EXTERNAL Limitations

The following are some limitations of using GLOBAL/EXTERNAL:

  • All global variables must be declared in the GLOBAL compilation unit.

  • Using GLOBAL/EXTERNAL results in slower link time.

  • Code that references global variables is not as efficient as code that does not use GLOBAL/EXTERNAL.

Using SUBPROGRAM with GLOBAL

The SUBPROGRAM with GLOBAL compiler options result in Pascal programs that are a mixture of subprogram and global compilation units. These compiler options must precede the reserved word PROGRAM.

Global variables declared here can be referenced in external compilation units.

When to Use SUBPROGRAM with GLOBAL

Use SUBPROGRAM with GLOBAL to allow multiple declarations of additional global variables instead of using just the outer block.

SUBPROGRAM with GLOBAL Advantages

When you use SUBPROGRAM with GLOBAL, you do not have to recompile the outer block if you are not using GLOBAL. This method of separate compilation is similar to using modules.

You do not have to share all variables with other languages, you can share only a few variables, if you wish.

If any of the global variables change, you only need to recompile the units that refer to them.

You can use this to put globals into shared libraries or XLs.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.