How to Compile a Simple Pascal Program in Ubuntu

Michelle Rogers
Want to compile a Pascal program? Whether it be for homework or personal use, this tutorial will show you how to quickly and easily get your program up and running.

After spending hours searching the web for answers, I discovered there is a major lack of information on the topic. As such I felt I should save others time and effort by sharing what I discovered after some experimentation. I believe the steps in this tutorial may be similar to that used to compile other programming languages, although you will have to obtain and install the appropriate compilers for them to work.

I am writing these instructions specifically for the free linux based operating system, Ubuntu 10.04 (www.ubuntu.com), but they may be the same or similar for older versions of Ubuntu or other linux variants.

Before you begin this tutorial you will need to install the Free Pascal Compiler available through the Ubuntu Software Center or by using some alternative method. The version I used for this tutorial is 2.4.0-2.

1. Open a text editor and type in the following:

program Hello;

begin

Writeln('Hello');

Write('world');

Readln;

end.

2. Save the file as hello.pas and save it in location you can find relatively easily.

3. Open the terminal and change the directory location to that of the hello.pas file. To change directories you need to type "cd" then the directories; for example:

name@name-desktop:~$ cd Documents
name@name-desktop:~/Documents$

4. Next you need to compile the program. You do so by typing in the following:

name@name-desktop:~/Documents$ fpc hello.pas

It should quickly display something similar to the following:

Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386

Copyright (c) 1993-2009 by Florian Klaempfl

Target OS: Linux for i386

Compiling hello.pas

Linking hello

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

6 lines compiled, 0.1 sec

Despite what the warning says, the file did compile and now you need to "install it" on your system.

5. If you look in the directory where the hello.pas file is located, you should now see a hello executable file and an object file called hello.o '" if you attempt to run the executable file you will notice that it doesn't work. Which is why you are now ready to do the following:

name@name-desktop:~/Documents$ sudo apt-get install hello

It will ask you to type in your password and after you do so, it should show something like this...

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following NEW packages will be installed:

hello

0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.

Need to get 0B/33.3kB of archives.

After this operation, 659kB of additional disk space will be used.

Selecting previously deselected package hello.

(Reading database ... 267190 files and directories currently installed.)

Unpacking hello (from .../archives/hello_2.4-3_i386.deb) ...

Processing triggers for install-info ...

Processing triggers for man-db ...

Setting up hello (2.4-3) ...

6. Now you are ready to run your program! Type "hello" in the terminal and press enter. Congratulations! You should now see "Hello, world!" in the terminal.

To learn to program in Pascal, I highly recommend the tutorials from http://www.learn-programming.za.net/programming_pascal_learn01.html.

You can download the Free Pascal Compiler from their website http://freepascal.org/, but it's much easier to use the Ubuntu Software Center.

Free Pascal (aka FPK Pascal) is a 32 and 64 bit professional Pascal compiler. The following operating systems are supported: Linux, FreeBSD, Haiku, Mac OS X/Darwin, DOS, Win32, Win64, WinCE, OS/2, Netware (libc and classic) and MorphOS.

2 Comments

Post a Comment
  • Thomas Tam6/4/2011

    thanks for your article. I have some experience with Turbo Pascal in MS operating system, and now I am new to Ubuntu Linux. I have been struggling to start Free Pascal in Ubuntu 10.04, with no success until I read your article today. I followed your example and was able to compile and run the "Hello world" program. Now I am a step closer to write simple programs in Free Pascal in Ubuntu. I have one immediate problem. In Turbo Pascal, I use , e.g. "writeln (lst, "print this.."); " , to send an output to the printer. But this command will not work in Free Pascal, giving me a compiling error, (compiler does not recognize 'lst'). I googled the problem but unable to find an answer. Can you please tell me how to send an output to a printer in Free Pascal for Ubuntu 10.04? Or can you direct me to a source for possible solution? Thanks again.

  • M Anthony9/30/2010

    Please go to http://www.associatedcontent.com/article/5827420/6_easy_steps_to_compile_and_run_a_pascal.html?cat=15 to view the updated article.

Displaying Comments

To comment, please sign in to your Yahoo! account, or sign up for a new account.