Units
In mikroPascal PRO for PIC, each project consists of a single project file and one or more unit files. Project file, with extension .mpppi
contains information about the project, while unit files, with extension .mpas
, contain the actual source code.
Units allow you to:
- break large programs into encapsulated parts 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 unit is stored in its own file and compiled separately. Compiled units are linked to create an application. In order to build a project, the compiler needs either a source file or a compiled unit file (.mcl
file) for each unit.
Uses Clause
mikroPascal PRO for PIC includes units by means of the uses
clause. It consists of the reserved word uses
, followed by one or more comma-delimited unit names, followed by a semicolon. Extension of the file should not be included. There can be at most one uses
clause in each source file, and it must appear immediately after the program (or unit) name.
Here’s an example:
uses utils, strings, Unit2, MyUnit;
For the given unit name, the compiler will check for the presence of .mcl
and .mpas
files, in order specified by the search paths.
- If both
.mpas
and.mcl
files are found, the compiler will check their dates and include the newer one in the project. If the.mpas
file is newer than.mcl
, a new library will be written over the old one; - If only
.mpas
file is found, the compiler will create the.mcl
file and include it in the project; - If only
.mcl
file is present, i.e. no source code is available, the compiler will include it as it is found; - If none found, the compiler will issue a “File not found” warning.
Main Unit
Every project in mikroPascal PRO for PIC requires a single main unit file. The main unit file 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 the Project Wizard, the Code Editor will display a new main unit. It contains the bare-bones of the Pascal program:
program MyProject; { main procedure } begin { Place program code here } end.
Nothing should precede the keyword program
except comments. After the program name, you can optionally place the uses
clause.
Place all global declarations (constants, variables, types, labels, routines) before the keyword begin
.
Other Units
Units other than main start with the keyword unit
. Newly created blank unit contains the bare-bones:
unit MyUnit; implementation end.
Other than comments, nothing should precede the keyword unit
. After the unit name, you can optionally place the uses
clause.
Interface Section
Part of the unit above the keyword implementation
is referred to as interface section. Here, you can place global declarations (constants, variables, labels and types) for the project.
You 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 unit. Prototypes must match the declarations exactly.
Implementation Section
Implementation section hides all irrelevant innards from other units, allowing encapsulation of code.
Everything declared below the keyword implementation
is private, i.e. has its scope limited to the file. When you declare an identifier in the implementation section of a unit, you cannot use it outside the unit, but you can use it in any block or routine defined within the unit.
By placing the prototype in the interface section of the unit (above the implementation
) you can make the routine public, i.e. visible outside of unit. Prototypes must match the declarations exactly.
What do you think about this topic ? Send us feedback!