Sheerpower Logo

Program Segmentation using Include Directives


When your code base becomes very large, dividing it into smaller sections can greatly aid in code maintenance. The %include directive is designed for this purpose. It tells the compiler to insert the contents of another file at that point in the program.

The %include directive supports two optional modifiers:

  • continue — Tells the compiler to proceed even if the include file is missing or cannot be opened. This enables conditional inclusion of platform-specific or optional modules.
  • once — Ensures that the file is included only once, even if referenced multiple times (directly or indirectly). This avoids duplicate declarations during nested includes.

Include files can also be nested—an include file can itself contain additional %include directives. This allows you to create reusable, modular layers of code for better organization.

Simple Include Example

Suppose we have a file named safe_common.spinc that defines some shared data and a utility routine:

full_name$ = 'Sally Sue' tax_rate = 6.5 routine do_it print 'Doing the routine called '; _routine end routine

You can include this file in your main program like this:

%include '@..\safe\safe_common' print 'The full name is: '; full_name$ do_it

Large business applications can easily contain hundreds of thousands of lines of code. These applications are greatly simplified by using many include directives. Typically, each include file contains all the variables and routines needed to perform a specific subtask.

Example: Nested Includes with once

The following example demonstrates how include files can reference other include files. We use the once modifier to ensure that files are not included more than once, even when nested.

// File: main.spsrc %include once "common.inc" print "Main program complete." log_message with msg$ = "From main" stop
// File: common.inc %include once "logging.inc" declare string name$ name$ = "Sheerpower" log_message with msg$ = "Loaded common.inc"
// File: logging.inc routine log_message with msg$ print "LOG: "; msg$ end routine

When main.spsrc is compiled, it automatically brings in both common.inc and logging.inc. Thanks to once, even if logging.inc is included from multiple files, it is compiled only once.

(Show/Hide %INCLUDE Directive Takeaways)
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.