How to correctly run Lazarus (the Free Pascal IDE) on Mac

This article describes how to correctly run Lazarus (the Free Pascal IDE) on Mac.

Environment:

- Hardware: MacBook Pro (Retina, 13-Inch, Late 2013)
- OS: OS X El-Capitan version 10.11.6 (15G31)
- Package Manager: MacPorts version 2.3.4

You will need to install [MacPorts](http://www.macports.org) and *selfupdate* as needed. MacPorts is used to install **gdb** (debugger). If you use Homebrew, though I have not confirmed it is working, you may use it to install **dbg** (there is a note on the Free Pascal website explaining about using Homebrew to install gdb).

Installation steps:

1. Install Prerequisites
2. Install Free Pascal
3. Install Lazarus
4. Notes

# Install Prerequisites

## Apple Developer tools

You need the Apple Developer tools, which are part of the XCode development environment. If you don't have it installed, you can execute the following terminal command:

```bash
$ sudo xcode-select --install
```

## Debugger (**gdb**)

You need a debugger to use Lazarus, the Free Pascal IDE. You can use **gdb** which was included in the Apple Developer tools but it is no longer included in the newer versions of the tools. In this article, I will explain how to install **gdb** using MacPorts. You may use Homebrew or other package manager or even build by your self.

There are two versions of gdb from MacPorts, one is the original GNU gdb and the other is customized for Mac environment. I will use the latter one. Install the gdb using the following terminal command:

```bash
$ sudo port install gdb-apple
```

The **gdb** command is installed at `/opt/local/bin/gdb`, which is linked to `/opt/local/bin/gdb-apple`.

The next thing you have to do is *codesigning* the gdb and make a modification to the file permission, in order to use it in Lazarus.

To *codesign* the **gdb**, you may refer to this [page](https://gcc.gnu.org/onlinedocs/gnat_ugn/Codesigning-the-Debugger.html).

After that, you need to change the file permission of the gdb command file, the `/opt/local/bin/gdb-apple`, using the following terminal command:

```bash
$ sudo chmod g-s /opt/local/bin/gdb-apple
```

# Install Free Pascal

You can install Free Pascal by downloading the installer from their [website](http://www.freepascal.org/download.var). You choose the appropriate edition for your Mac. When the installation of Free Pascal is finished, you can confirm the compiler using terminal commands.

Suppose you have written a simple pascal code like this:

```pascal:hello.pas
program hello
begin
  writeln('Hello, world!');
end.
```

Then, from terminal, you can run the following command:

```bash
$ fpc hello
Free Pascal Compiler version 3.0.0 [2015/11/14] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Darwin for i386
Compiling hello.pas
Assembling (pipe) hello.s
Linking hello
4 lines compiled, 0.3 sec
```

After that, you can run the program as simple as this:

```bash

$ ./hello
Hello, world!
```

# Install Lazarus

To install Lazarus, you can download the installers from this [page](http://www.lazarus-ide.org/index.php?page=downloads).

<img width="652" alt="Lazarus_-_Browse__Lazarus_Mac_OS_X_i386_Lazarus_1_6_at_SourceForge_net.png" src="https://qiita-image-store.s3.amazonaws.com/0/100827/c3d002e1-2d03-6826-438a-dea9d811ece7.png">

You need to install three components here. First, the Free Pascal compiler, which we have done in the previous step above. The rests are the Free Pascal source codes **fpcsrc** and the Lazarus application **lazarus**  itself.

Thats it. We can now start using Lazarus to develop program using Free Pascal.

# Notes

When you run a project from within the Lazarus IDE, you may encounter this dialog:

<img width="474" alt="Lazarus_RunWarning.png" src="https://qiita-image-store.s3.amazonaws.com/0/100827/993c3285-3bdb-3bc3-f01d-0a8e67fd733a.png">

It seems that there is a bug in the **dbg**. You can just ignore this dialog. You can also configure to not displaying this dialog again by setting the **WarnOnTimeout** to **False** in the IDE Options:

<img width="800" alt="Lazarus_IDE_Options.png" src="https://qiita-image-store.s3.amazonaws.com/0/100827/3d4e81a0-5f41-b035-d14e-f1fae51f8123.png">