Binding Modules into a Program - IBM - RPG

Binding is the process of creating a runnable ILE program by combining one or more modules and optional service programs, and resolving symbols passed between them. The system code that does this combining and resolving is called a binder on the iSeries system.

As part of the binding process, a procedure must be identified as the startup procedure, or program entry procedure. When a program is called, the program entry procedure receives the parameters from the command line and is given initial control for the program. The user’s code associated with the program entry procedure is the user entry procedure.

If an ILE RPG module contains a main procedure, it implicitly also contains a program entry procedure. Therefore, any ILE RPG module may be specified as the entry module as long as it is not a NOMAIN module.

This gives an idea of the internal structure of a program object. It shows the program object TRPT, which was created by binding the two modules TRANSRPT and TRANSSVC. TRANSRPT is the entry module.

Structure of Program TRPT
Structure of Program TRPT

Within a bound object, procedures can interrelate using static procedure calls. These bound calls are faster than external calls. Therefore, an application consisting of a single bound program with many bound calls should perform faster than a similar application consisting of separate programs with many external interapplication calls.

In addition to binding modules together, you can also bind them to service programs (type *SRVPGM). Service programs allow you to code and maintain modules separately from the program modules. Common routines can be created as service programs and if the routine changes, the change can be incorporated by binding the service program again. The programs that use these common routines do not have to be recreated.

Using the CRTPGM Command

The Create Program (CRTPGM) command creates a program object from one or more previously created modules and, if required, one or more service programs. You can bind modules created by any of the ILE Create Module commands, CRTRPGMOD, CRTCMOD, CRTCBLMOD, or CRTCLMOD.

Note: The modules and/or service programs required must have been created prior to using the CRTPGM command.

Before you create a program object using the CRTPGM command, you should:

  1. Establish a program name.
  2. Identify the module or modules, and if required, service programs you want to bind into a program object.
  3. Identify the entry module.
  4. You indicate which module contains the program entry procedure through the ENTMOD parameter of CRTPGM. The default is ENTMOD(*FIRST), meaning that the module containing the first program entry procedure found in the list for the MODULE parameter is the entry module.

    Assuming you have only one module with a main procedure, that is, all modules but one have NOMAIN specified, you can accept the default (*FIRST). Alternatively, you can specify (*ONLY); this will provide a check that in fact only one module has a main procedure. For example, in both of the following situations you could specify ENTMOD(*ONLY).

    • You bind an RPG module to a C module without a main() function.
    • You bind two RPG modules, where one has NOMAIN on the control
      specification.

    Note: If you are binding more than one ILE RPG module with a main procedure, then you should specify the name of the module that you want to receive control when the program is called. You can also specify *FIRST if the module with a main procedure precedes any other modules with main procedures on the list specified for the MODULE parameter.

  5. Identify the activation group that the program is to use. Specify the named activation group QILE if your program has no special requirements or if you are not sure which group to use. In general, it is a good idea to run an application in its own activation group. Therefore, you may want to name the activation group after the application. Note that the default activation group for CRTPGM is *NEW. This means that your program will run in its own activation group, and the activation group will terminate when the program does. Whether or not you set on LR, your program will have a fresh copy of its data the next time you call it. For more information on activation groups see “Specifying an Activation Group”.

To create a program object using the CRTPGM command, perform the following steps:

  1. Enter the CRTPGM command.
  2. Enter the appropriate values for the command parameter.

This lists the CRTPGM command parameters and their default values.

Parameters for CRTPGM Command and their Default Values
Parameters for CRTPGM Command and their Default Values

Once you have entered the CRTPGM command, the system performs the following actions:

  1. Copies listed modules into what will become the program object, and links any service programs to the program object.
  2. Identifies the module containing the program entry procedure, and locates the first import in this module.
  3. Checks the modules in the order in which they are listed, and matches the first import with a module export.
  4. Returns to the first module, and locates the next import.
  5. Resolves all imports in the first module.
  6. Continues to the next module, and resolves all imports.
  7. Resolves all imports in each subsequent module until all of the imports have been resolved.
  8. If any imports cannot be resolved with an export, the binding process terminates without creating a program object.
  9. Once all the imports have been resolved, the binding process completes and the program object is created.

Note: If you have specified that a variable or procedure is to be exported (using the EXPORT keyword), it is possible that the variable or procedure name will be identical to a variable or procedure in another procedure within the bound program object. In this case, the results may not be as expected. See ILE Concepts for information on how to handle this situation.

Binding Multiple Modules
This example shows you how to use the CRTPGM command to bind two ILE RPG modules into a program TRPT. In this program, the following occurs:

  • The module TRANSRPT reads each transaction record from a file TRNSDTA.
  • It then calls procedure Trans_Inc and Proc_Name in module TRANSSVC using bound calls within expressions.
  • Trans_Inc calculates the income pertaining to each transaction and returns the value to the caller
  • Proc_Name determines the product name and returns it
  • TRANSRPT then prints the transaction record.

Source for TRANSRPT, TRANSSVC, and TRNSDTA is shown.

  1. First create the module TRANSRPT. Type: CRTRPGMOD MODULE(MYLIB/TRANSRPT)
  2. Then create module TRANSSVC by typing: CRTRPGMOD MODULE(MYLIB/TRANSSVC)
  3. To create the program object, type: CRTPGM PGM(MYLIB/TRPT) MODULE(TRANSRPT TRANSSVC) ENTMOD(*FIRST) ACTGRP(TRPT)

Using the CRTPGM Command
The CRTPGM command creates a program object TRPT in the library MYLIB. Note that TRANSRPT is listed first in the MODULE parameter. ENTMOD(*FIRST) will find the first module with a program entry procedure. Since only one of the two modules has a program entry procedure, they can be entered in either order.

The program TRPT will run in the named activation group TRPT. The program runs in a named group to ensure that no other programs can affect its resources.

This shows an output file created when TRPT is run.

File QSYSPRT for TRPT
File QSYSPRT for TRPT

Additional Examples
For additional examples of creating programs, see:

  • “Binding to a Program”, for an example of binding a module and a service
    program.
  • “Sample Source for Debug Examples” for an example of creating a program consisting of an RPG and C module.

Related CL Commands
The following CL commands can be used with programs:

  • Change Program (CHGPGM)
  • Delete Program (DLTPGM)
  • Display Program (DSPPGM)
  • Display Program References (DSPPGMREF)
  • Update Program (UPDPGM)
  • Work with Program (WRKPGM)

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM - RPG Topics