![]() |
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.
Suppose we have a file named safe_common.spinc
that
defines some shared data and a utility routine:
You can include this file in your main program like this:
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.
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.
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.
%include
to divide large programs into
manageable, modular files.
continue
allows the program to compile even if the
include file is missing—ideal for optional or conditional code.
once
ensures the same file is not included
multiple times, even across nested includes.
%include
directives, enabling layered and reusable program structures.
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. |