In the world of IBM i, copybooks are like treasure maps, usually buried in a source physical file, but sometimes they're hiding in the cool, modern IFS spots. These little nuggets of code gold can be plundered for CL programs, just by copying and pasting the copybook's secrets right into your program's treasure chest.
In RPG copybooks are referred using the /COPY or /INCLUDE statements. The contents of the copybook are physically copied into the program, and the program is compiled with the copied code
There are some differences between copy and include: The /COPY and /INCLUDE directives have the same purpose and the same syntax, but are handled differently by the SQL precompiler. SQL will check the RPGPPOPT parameter to see how deep it can go but RPG will always load all from multiple levels. This can lead to some head scratching moments… I know it has for me.
It's worth noting that the /INCLUDE
directive is a more modern directive, and it's recommended to use it instead of /COPY
whenever possible, as it provides more flexibility and better performance.
If your program has embedded SQL then stick with /copy (if you don't want to worry about compiling with RPGPPOPT) but if it's a native RPGLE program then you can use either.
copybook - header.rpgleinc
This copybook contains a control option, which we want to be added to every RPG program:
// Common Header Specifications
ctl-opt
main(mainline)
optimize(*full)
option(*nodebugio:*srcstmt:*nounref)
pgminfo(*pcml:*module)
indent('| ');
RPG Program - HELLOINC-Helloworld_using_copybook.rpgle
This sample of RPG code uses two copybook /include statements:
ctl-opt copyright('HelloAdvanced | V.000 | Sample Stylised RPG Program');
/include global_variables.rpgleinc
Remember, we don't need to add the folder prefix so the copybook location because CODE for IBM I and the BOB Better Object Builder knows where it lives by the include paths defined in the Project Explorer.