Getting Started with Lazarus at TU/e

Lazarus is being used in the 2008-2009 introductory programming course for first-year computer science students at Eindhoven University of Technology. The following information is mainly focused on use under Windows. There may be some small differences for use on other platforms.


What is Lazarus?

Lazarus together with FreePascal is a set of open-source software development tools running on various platforms, including (but not restricted to) Linux, Mac OS X, and Windows. The supported programming language is Object Pascal, which is a Pasal extension that includes object-oriented (OO) features. Both Lazarus and FreePascal are themselves written in Object Pascal.

Lazarus is an Integrated Development Environment (IDE) with a graphical user interface for Rapid Application Development (RAD). The IDE controls the project inspector, source editor, object inspector, code explorer and code browser, compiler, linker, debugger, etc. For development of applications with a Graphical User Interface (GUI), Lazarus offers the Lazarus Component Library (LCL).

FreePascal (FPC) is a compiler for various Pascal dialects targeting multiple platforms. FPC can also be used separately on the command line (Windows: in a command window). FreePascal includes extensive libraries with extra functionality, such as the Run-Time Library (RTL) and Free Component Library (FCL).

Lazarus resembles CodeGear's Delphi, which is a commercial RAD IDE for Object Pascal. FreePascal has a Delphi compatibility mode. However, Delphi and Lazarus projects are not interchangeable. Lazarus does have a tool to help convert a Delphi project into a Lazarus project.

Lazarus and FreePascal documentation

On-line documentation for Lazarus is available in the following places:

On-line documentation for FreePascal:

FreePascal as command-line compiler

FreePascal can also be invoked on the command-line (Windows: in a command window) to compile a Pascal program (typically a console application) into an executable:

fpc <file.pas>

NOTE: This assumes that your execution path includes the path to fpc. Assuming a standard installation of Lazarus on Windows, your execution path must include \lazarus\fpc\<version>\bin\<target>\. In this way, most Pascal programs can be compiled into executables.

Advantages of using the command-line compiler are:

However, development of applications with a graphical user interface (GUI) is easier with Lazarus.

Starting the Lazarus Integrated Development Environment (IDE)

On Windows:

Start -> All Programs -> Lazarus -> Lazarus
This starts the Lazarus Integrated Development Environment.
Help is available under the Help menu (also see Lazarus and FreePascal documentation).
Experimentation is encouraged (pull down those menus :-).
Context menus are opened by `right'-clicking on items.

NOTE: By default, Lazarus starts up with a blank project for developing a GUI application (typically a Form with a grid of dots is shown). For `simple' programming tasks, this is NOT what you want. Choose Project->Close Project to get rid of this blank project (do NOT save anything).

Opening an existing project

All files needed to build a program are gathered in, what is called, a project. A Lazarus Project Information file with extension .lpi defines which files belong to a project. It is recommended that you put each project in a separate folder.

To open an existing Lazarus project

Double-click a Lazarus project file
This starts Lazarus and opens the project.

NOTE: Even though the project is now open, it is possible that you do not see any windows for this project. Also see Viewing the program in a project.

Projects can also be opened from within the Lazarus IDE:

Project -> Open Project, or Ctrl+F11
Select an existing Lazarus Project Information file from the dialog box to open it.
Project -> Open Recent Project ...
This reopens a recently opened project.

You can try the project GetalKlutser (in Dutch). The zip archive contains one folder named `GetalKlutser' with a Lazarus Project Information file `GetalKlutser.lpi' and all related files for a console application. (Here is a GUI version for Windows: NumberMunger.)

Project -> Project Inspector
Opens the Project Inspector window, which shows what other files (e.g. units) are in the project. `Simple' projects have no such other files.

Viewing the program in a project

Make sure that the project is open.

Project -> View Source
Opens the Lazarus Source Editor window, which displays the program texts (=source code) of the various parts of the program.
You can also open source files in a project by double clicking them in the Project Inspector (accessible under the Project menu).
NOTE: You may still have to open the Source Editor:
View -> Source Editor
Opens the Lazarus Source Editor.
NOTE: A project can contain multiple source files. One of them is the main program (typically with extension .lpr, the others are units (typically with extension .pas).
Right-click on the tab or the code in the Source Editor and select the menu item 'Close Page'
This closes the related source file.
Right-click on the code in the Source Editor and select `Show Line Numbers'
In the left margin, all lines are numbered. Select again to hide line numbers.
The line number and column of the cursor are shown in the lower-left corner.

Printing the program in a project

Make sure that the project is open and the program source is visible in the Source Editor.

File -> Print..., or Ctrl+P
A dialog box appears to change some settings.
The program's source text is printed with lines numbers.

Running the program in a project

Make sure the project is open in the IDE.

Run -> Run, or F9, or the `Play triangle' icon on main SpeedBar
If necessary, the FreePascal Compiler and Linker are invoked to build an executable (Windows: with extension .exe).

A Console Application (also see Changing the options of a project) automatically opens its own console window (some, or even all, Lazarus windows may be hidden, also see Changing the IDE preferences). When the program finishes, its window automatically closes and the hidden Lazarus windows are shown again. It is often convenient to include the lines

; Write('Type return to dismiss this window: ')
; ReadLn
at the end of a console application. (The write statement can be omitted, but keep in mind you need to type return to terminate the program.) Before typing return, you can use the Edit->Mark and Copy buttons of the window (under the icon in the top left corner) to save program output produced in the window (e.g. to paste it into some other editor window where you are making a report).

Tracing the program's execution

When a program is running `on its own', you can only observe and control it through the communication actions that it contains. By using the debugger, you can run a program in a more controlled way: in `small' steps, with the possibility to observe the values of variables.

Run -> Step Into, or F7, or `Step Into' icon on main SpeedBar
Run -> Step Over, or F8, or `Step Over' icon on main SpeedBar
The first time you invoke Step Into or Step Over (see below for the difference between these two), the program's console window opens (after recompiling if necessary), and the program stops at the very first statement. The highlighted (gray) line in the program's source window is the next line of code to be executed. Each next time you invoke Step Into or Step Over, one line of code is executed and immediately after that the program is stopped again.

NOTE: It is necessary to provide input in the program's console window when a read or readln statement is executed; otherwise, stepping will NOT continue.

Program variables can be inspected by pointing at the variable name in the Source Editor window, or by adding them to the Watch List (see Watches and Add Watch below). They can be modified through the Evaluate/Modify window (see below).

Step Into executes one line of the program. When that line contains a function or procedure call, execution stops at the first line of the body of the first call. However, this it also `steps into' system calls. Usually, it is best to use Step Over, and only use Step Into when you really want to delve into details.

Step Over executes one line of the program. When that line contains a function or procedure call, these are executed completely without intermediate stopping.

Run -> Stop, or Ctrl+F2
The next Run/Step will now start at the beginning of the program.
View -> Debug Windows -> Watches
Brings up the Watch List window, which shows a list of user-supplied expressions (e.g. variables) and their current values. Double click the topmost empty line to add a new expression.
Run -> Evaluate/Modify, or Ctrl+F7 (only available while a program is running)
Opens the Evaluate/Modify window, where expressions can be evaluated on the fly in the current state, and where program variables can be modified.
Run -> Run to Cursor, or F4
Execute statements, until arriving at the line with the cursor
View -> Debug Windows -> Call stack
Brings up the Call Stack window, which lists the order in which the currently active procedures and functions have been called and what their parameter values are.

Breakpoints

In larger programs (more than a few screens of code), it may be helpful to set breakpoints when tracing the program's execution.

Click in the margin on the left of a line of code
This sets or removes a breakpoint, where execution will be interrupted when running the program.
A breakpoint is visualized by a red dot.
Right-click on the code
This brings up a contextual menu with an item labeled 'Debug' and a submenu item for setting a breakpoint.

Changing the program in a project

Make sure the project is open in the IDE and visible in the Source Editor. The Lazarus Source Editor is a fairly standard text editor with syntax highlighting and some other features (also see Changing the IDE preferences), such as an extensive Search/Replace commands (see Search menu). You can change the program text in this window.

File -> Save, or Ctrl+S, or `Save to disk' icon on main SpeedBar
Saves changes to disk.
NOTE: Bad programming errors may cause your computer to `crash' and thereby lose all unsaved changes.

Compiler and runtime messages

When the FreePascal Object Pascal Compiler or Linker detects errors or reasons for warnings, these are listed the Messages window. Clicking a message in the bottom part, highlights the corresponding line in the Editor window. The message briefly explains what is the matter. Additional help can be obtained by selecting the message and typing F1.

NOTE: The `real' cause of an error may be located in a different place from where the tools mark it (typically, earlier in the file).

When a runtime error (e.g. division by zero) occurs while a program executes from within the IDE, an error message is shown in a separate window, and the offending line is highlighted in the Source Editor. See Tracing the program's execution for ways of obtaining more information about the error state.

However, when a console-application program is run from the command line, an error message appears on the console. The error message includes a stack trace (with line numbers, if the project was derived from the `TUE Console Application', as explained next).

Creating a new project

This is slightly complicated (but making it easier is even harder :-)

  1. Open the Lazarus project TUeConsoleApp.lpi in the folder TUE Console Application, or an existing Lazarus project that was derived earlier from the `TUeConsoleApp' project. Also see Opening an existing project.
  2. Use Project -> Save Project As to save this project in another folder under another name.
    You can create a new folder from the Save Project As dialog box. To change the name, replace the TUeConsoleApp part of the file name before clicking Save. Lazarus automagically changes the program name in the file as well (next to the keyword program).
  3. The newly made copy becomes the currently active project.
  4. See Changing the program in a project for instructions on changing the program and saving the changes.

NOTE:

Changing the options of a project

The project options for console-application projects started from `TUE Console Application' (see Creating a new project) have been set appropriately for `beginners'. The project options for application projects started in other ways (e.g. the default blank project when Lazarus is started from the Start menu) are usually less appropriate and need adjustment.

The project's options are divided into project options proper and compiler options

Project -> Project Options..., or the `Option' button in the Project Inspector
Opens the Project Options window.
There are no particular recommendations for these project options.
Project -> Compiler Options...
Opens the Compiler Options window.
Recommended settings are:
Paths:
  • No paths need to be set
  • LCL Widget Type: default
Parsing:
  • Delphi 2 Extensions
  • Include Assertion Code
  • Delphi Compatible
  • Use Ansi Strings
Code:
  • Check: I/O, Range, Overflow, Stack, Verify method calls
  • Generate: Normal Code
  • Optimizations: Level 0
Linking:
  • Debugging: Display Line Numbers in Run-time Error Backtraces
  • Target OS specific options: only select this for Windows GUI applications
Messages:
  • Show Errors, Warnings, Notes, Hints, general info
  • Stop after number of errors: 1

Changing the IDE preferences

The behavior of the IDE can be modified through a large set of Options.

Environment -> Environment Options...
The Environment Options dialog window opens.
Some recommended settings are:
Desktop:
  • Autosave: select `Editor files', `Project'
Windows:
  • Select: Hide IDE windows on run
Environment -> Editor Options...
The Editor Options dialog window opens.
Some recommended settings are:
General
  • Use syntax highlight
  • 2   Block indent
Code Folding
  • Off


© 2008, Tom Verhoeff (TU/e, Faculteit Wiskunde en Informatica)
Feedback about this page is welcome