Skip to content

Hello, World!

How would you like to do your Hello World?

Using the Lazarus IDE

Note

This section assumes you have correctly set up the following for your OS.

  1. The Free Pascal Compiler.
  2. The Lazarus IDE.

Create a Project

  1. Launch Lazarus IDE.
  2. Create a new Project.
    • On the top menu bar, click Project -> Simple Program -> OK.
  3. Save this Project.
    • Click Project -> Save Project.
    • Save the Project file as HelloWorld.lpi in a new folder.
    • Note: Lazarus will save the main source file as HelloWorld.lpr.

You will see a simple program in the Source Editor window. The program's name will be the same as the Project's name, as shown below.

1
2
3
4
program HelloWorld;

begin
end.

Add Code

  1. Now insert the following lines between begin and end..
WriteLn('Hello, World!');
ReadLn;
  • The WriteLn function prints text on the console.
  • The ReadLn in the code prevents the console from being closed automatically.
  1. Add the following compiler directives after the program declaration.
{$mode objfpc}{$H+}{$J-}

Note

Make sure to have this line in all your Object Pascal codes.

{$mode objfpc}{$H+}{$J-}

Your final code would look as follows.

1
2
3
4
5
6
7
8
program HelloWorld;

{$mode objfpc}{$H+}{$J-} // Add this line in your object pascal codes.

begin
  WriteLn('Hello, World!');
  ReadLn;
end.
  1. Press Ctrl+S to save the code.

Compile & Run

Press F9 to run the compile and run the program.

You should be able to see a console open with Hello World in it.

Press the Enter key to exit the progam, which also closes the console.

Using the CLI

Note

This section assumes you have correctly set up the Free Pascal Compiler and the fpc is in your PATH.

Create a .pas File & Add Code

  1. Launch your favourite text editor
  2. Create a new file and put the following snippet in it.
1
2
3
4
5
{$mode objfpc}{$H+}{$J-} 

begin
    WriteLn('Hello, World!');
end.
  1. Save it as HelloWorld.pas.

Compile & Run

Windows CLI

On Windows, compile and run as follows.

> fpc HelloWorld.pas && HelloWorld.exe

Tip

If running fpc from CLI doesn't work, try one of the following options.

  1. Supply the full path to the fpc.
  2. Put the fpc/bin/<architecture> to your PATH then compile and run again.
  3. Consider using Lazarus IDE instead.
  4. Are you a VSCode or VSCodium user? Make sure to setup Pascal by Allesandro Fragnani properly.
  5. Have you considered OmniPascal?

Linux CLI

On Linux, compile and run as follows.

$ fpc HelloWorld.pas && ./HelloWorld