| United States-English |
|
|
|
![]() |
HP Pascal/HP-UX Programmer's Guide > Chapter 2 Program Structure Separate Compilation |
|
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:
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. 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. 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. Some advantages to separate compilation with modules are:
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. SUBPROGRAM is recommended for use in compilation units where the global variables won't change much. 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. 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. 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:
The EXTERNAL compiler option:
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. The following are some advantages of using GLOBAL/EXTERNAL:
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. Use SUBPROGRAM with GLOBAL to allow multiple declarations of additional global variables instead of using just the outer block. 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. |
||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||