Editing and Compiling Pascal Programs

To compile and run Pascal programs, you will need

  1. the Free Pascal compiler

  2. a text editor or IDE (integrated development environment) for editing your code

Installing Free Pascal

If you are in the lab here at the MFF, you don't need to do anything: Free Pascal is already installed on all UNIX and Windows machines.

To install Free Pascal on your own computer:

on Linux

You should be able to get Free Pascal from your package manager. On Ubuntu, type "sudo apt-get install fpc".

on macOS

This will take some time because you must first download and install Apple's Xcode, which is a required dependency. You can get Xcode for free from the App Store on macOS. It is a 5 Gb download, and will take 20-30 minutes to install after it's downloaded!

After you've installed Xcode, open a terminal window and run "xcode-select --install". Then go here, choose a mirror (except SourceForge) and download the first installation package at the bottom of the page. Run the downloaded installer.

on Windows

Go here, select a mirror (except SourceForge) and click the link under "Download native compiler". Run the downloaded installer.

Using a text editor or IDE

You may use any editor you like to edit Pascal code.

Geany

If you are unsure, I recommend Geany. It's a programmer's editor with built-in support for Free Pascal, which makes it very easy to use.

Installing Geany

On Linux, you should be able to install Geany from your package manager. On Ubuntu, type "sudo apt-get install geany".

On macOS or Windows, you can download a Geany installer from here.

Geany is not yet preinstalled on Windows machines in the computer lab at the MFF. But you can install it yourself there easily. There is only one problem: the Geany user interface will appear in Czech!

To fix that, download geany_english.bat and save it to the desktop. When you click this file, Geany will launch in English.

Building Pascal programs in Geany

To build a Pascal program in Geany:

  1. Use File/New (or type ctrl-N, or click the new file toolbar icon) to create a new file.

  2. Enter your program text. You might try a "hello, world" program first.

  3. Save the file somewhere, choosing a filename ending in '.pas'. You cannot build before you save.

  4. Use Build/Compile to build your program (or press F8, or click the Compile icon.

  5. To run your program, choose Build/Execute, or press F5, or click the Run icon.

Formatting code in Geany

On Linux or macOS, you can configure Geany so that it can automatically format your Pascal code with just a couple of keystrokes, using the ptop code formatter that comes with Free Pascal. To set this up:

  1. Download pformat (a shell script) and ptop.cfg and save them in some directory.

  2. Make pformat executable. (From the command line, you can run chmod u+x pformat .)

  3. In Geany, choose Edit→Format→Send Selection to→Set Custom Commands.

  4. In the Set Custom Commands dialog, click Add. In the Command field, type either just pformat (if it is in your PATH) or the full path to pformat (if it is not).

Now, to format Pascal code in a Geany editor window:

  1. Press Ctrl+A (Linux) or Command+A (macOS) to select your entire program.

  2. Press Ctrl+1 (Linux) or Command+1 (macOS). Your code will be reformatted.

Formatting code from the command line

On Windows, the steps in the previous section will not work, but you can still reformat your Pascal code from the command line:

  1. Download ptop.cfg and save it in some directory.

  2. To format the program my_program.pas, type

C:> ptop -c ptop.cfg my_program.pas my_program_out.pas

Your formatted code will appear in the file my_program_out.pas.

Lazarus

You could use Lazarus, a Pascal IDE that's preinstalled on lab machines. It is a larger and more complex program than editors such as Geany.

To build a Pascal program in Lazarus:

  1. Launch Lazarus and close all the windows that appear except the top window with the menu bar.

  2. Choose File/New... In the dialog that appears, choose Simple Program and press OK.

  3. Enter your program text. If you want to run your program directly from Lazarus, you should add this line at the end of your program:

    readLn;

    That's because otherwise the window will close instantly when your program finishes executing. This extra line will prevent the window from closing until you press Enter.

  4. On Windows and possibly macOS, you can now launch your program from Lazarus via the Run/Run command. On UNIX I have been unable to get this to work, so you'll need to run your program from the command line as described under "Other editors" below.

Other editors

I do not recommend the Free Pascal IDE, which is preinstalled on lab machines but which uses an ugly ancient text-based interface.

You may use any other editor you like. If you cannot compile Pascal programs from within your editor, you'll need to do that on the command line. To do that, open a terminal window, use the cd command to move to the directory where your program file is stored, and type

$ fpc myprog.pas

If there are errors, you'll need to look at the line number in the error messages and use your editor to manually find these lines. That's a pain, which is why I recommend using Geany, Lazarus or another editor with built-in support for compiling Pascal programs.

Compiling Programs

Geany and Lazarus know how to launch the Free Pascal compiler automatically. Or you can run it manually from the command line, like this:

$ fpc hello.pas

On every successful build, you'll see this warning message:

/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?

This warning is benign and can be ignored. (It has been fixed in the development build of FPC 3.1.1, which has not yet become an official release.)

If you don't want to see this warning on Linux or macOS, create a wrapper script fpc1 containing this:

#!/bin/bash
set -o pipefail
fpc $* |& grep -v 'contains output sections'

If you use fpc1 to build, this warning will be skipped.