Modules
In mikroBasic PRO for PIC32, each project consists of a single project file and one or more module files. The project file, with extension .mbp32 contains information on the project, while modules, with extension .mbas, contain the actual source code. See Program Organization for a detailed look at module arrangement.
Modules allow you to:
- break large programs into encapsulated modules that can be edited separately,
- create libraries that can be used in different projects,
- distribute libraries to other developers without disclosing the source code.
Each module is stored in its own file and compiled separately; compiled modules are linked to create an application. To build a project, the compiler needs either a source file or a compiled module file for each module.
Include Clause
mikroBasic PRO for PIC32 includes modules by means of the include clause. It consists of the reserved word include, followed by a quoted module name. Extension of the file should not be included.
You can include one file per include clause. There can be any number of the include clauses in each source file, but they all must be stated immediately after the program (or module) name.
Here’s an example:
program MyProgram include "utils" include "strings" include "MyUnit" ...
For the given module name, the compiler will check for the presence of .emcl and .mbas files, in order specified by search paths.
- If both
.mbasand.emclfiles are found, the compiler will check their dates and include the newer one in the project. If the.mbasfile is newer than the.emcl, then.mbasfile will be recompiled and new.emclwill be created, overwriting the old.emcl. - If only the
.mbasfile is found, the compiler will create the.emclfile and include it in the project; - If only the
.emclfile is present, i.e. no source code is available, the compiler will include it as found; - If none of the files found, the compiler will issue a “File not found” warning.
Main Module
Every project in mikroBasic PRO for PIC32 requires a single main module file. The main module is identified by the keyword program at the beginning. It instructs the compiler where to “start”.
After you have successfully created an empty project with Project Wizard, Code Editor will display a new main module. It contains the bare-bones of the program:
program MyProject ' main procedure main: ' Place program code here end.
Other than comments, nothing should precede the keyword program. After the program name, you can optionally place the include clauses.
Place all global declarations (constants, variables, labels, routines, structures) before the label main.
Other Modules
Modules other than main start with the keyword module. Newly created blank module contains the bare-bones:
module MyModule implements end.
Other than comments, nothing should precede the keyword module. After the module name, you can optionally place the include clauses.
Interface Section
Part of the module above the keyword implements is referred to as interface section. Here, you can place global declarations (constants, variables, labels, routines, structures) for the project.
Do not define routines in the interface section. Instead, state the prototypes of routines (from implementation section) that you want to be visible outside the module. Prototypes must exactly match the declarations.
Implementation Section
Implementation section hides all the irrelevant innards from other modules, allowing encapsulation of code.
Everything declared below the keyword implements is private, i.e. has its scope limited to the file. When you declare an identifier in the implementation section of a module, you cannot use it outside the module, but you can use it in any block or routine defined within the module.
By placing the prototype in the interface section of the module (above the implements) you can make the routine public, i.e. visible outside of module. Prototypes must exactly match the declarations.
What do you think about this topic ? Send us feedback!



