Howdy World (Hello World on steroids)

From Lazarus wiki
Revision as of 20:26, 8 June 2011 by Eny (talk | contribs) (New page: {{newpage}} This Wiki chapter is a tutorial for Lazarus. It explains the first steps to get a working piece of software and explains some of the best practices along the way. The end resul...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Template:newpage This Wiki chapter is a tutorial for Lazarus. It explains the first steps to get a working piece of software and explains some of the best practices along the way. The end result is twofold (hopefully): the reader understands the basic concepts to build software with Lazarus and she has an actual piece of working software that can be embedded in other programs: a calculator. A calculator is fairly easy to implement and everyone understands it’s concepts. So no need to describe a lengthy business case beforehand. The calculator is limited to integer calculations, but can easily be extended. This tutorial does not describe the installation process of Lazarus. It is assumed that Lazarus is installed and ready for use (preferably the latest, stable version which at the moment of writing is Lazarus 0.9.30 with Freepascal v2.4.2). This tutorial is more or less platform independent (but targeted at thick clients). All screenshots were made on a Windows XP PC; hence the blue/red/grayish color scheme.

Let’s get started

It’s best to create a separate directory for every new project. So before getting our feet wet, let’s create a directory to save our Calculator project in (do this with your OS application of choice). This directory will be the root for all files created for the project.

  • Create a new directory for the demo project (let’s call it $HOME/CalculatorDemo).

(Text that describes actions that must be executed is bulleted like the previous line.)

With this out of the way it’s time to start a new Lazarus project.

  • Start Lazarus.
  • From the menu choose Project/New Project…
  • In the dialog that is presented select Application and press OK (if the IDE complains about unsaved changes, press No).

To make sure that all files of our new project end up in the right directory the project must be saved first.

  • From the menu choose File/Save All.
  • Select the directory that was created previously ($HOME/CalculatorDemo).
  • Enter a new project name: CalculatorDemo
  • Press Save (or whatever is the Save button in your own language)
  • The IDE wants to save the main form as well: enter ’’ufrmMain’’ as the forms unit name.
  • The IDE asks if the filename should be changed to a lowercase name. Confirm this by pressing the button ‘Rename to lowercase’.

In theory the program that is thus created is a piece of valid software that can be executed. Before compiling it for the first time, two changes are recommended: assigning new locations (directories) for the compiled units and target filename.

  • From the menu choose Project/Project Options…
  • Select Compiler Options/Paths (click on the node in the treeview)
  • Put the text ‘‘bin\’‘ before the target filename (or ‘‘bin/’‘ for a *nix environment)
  • Also note the ‘‘lib’‘ prefix for the ‘unit output directory’ (don’t change it).
  • Press OK

The image below is how this would look like on a Windows machine. tutcal project options.png

By doing this the project folder will not be cluttered with output that is generated by the compiler. The files in the project folder are required to (re-) build the program. Everything in the ’’lib’’ and ’’bin’’ directories can be deleted when archiving the project.

Now as a first test the project can be compiled and run.

  • Press F9 (the shortcut for compile, build and run the program)

If all went well a blank window is displayed. Now we know that a solid basis is created on which we can start building the program.

  • End the running program by clicking on the Close icon (this depends on the OS).