How To Install Free Pascal Compiler

Posted on

English (en)中文(中国大陆)‎ (zh_CN)

  • 2Linux
    • 2.3FPC sources
  • 3FreeBSD
  • 4OpenBSD
  • 6Windows
    • 6.2Installing from source
      • 6.2.1Obtaining the source with a command line svn client
      • 6.2.2Obtaining the source with the GUI TortoiseSVN client
  • 7macOS
    • 7.1Installing an FPC release from the official macOS packages
    • 7.2Other installation options
    • 7.3Installing from source

Overview

There are a number of ways to install the Free Pascal Compiler and its libraries on your system. The current version and trunk, and sometimes beta and release candidates, are available directly from the FPC website (tar, exe, rpm, dmg, pkg files). In addition, versions of FPC are provided (including rpm and deb files) from the Lazarus download site. Linux users can almost certainly find FPC in their distribution's repositories but may find that it is outdated.

It is possible to build FPC from source, typically each release needs to be built with the previous release. It is possible to install FPC in the application space (often requiring root or administrator access) or in a your own user space. These things depend on your particular operating system.

There is an extensive discussion of how to install and build Free Pascal compilers available here http://www.stack.nl/~marcov/buildfaq.pdf - it may be a little too detailed for some users, but it is very comprehensive.

Free Pascal − it is a free compiler for running Pascal and Object Pascal programs. Free Pascal compiler is a 32- and 64-bit Turbo Pascal and Delphi compatible Pascal compiler for Linux, Windows, OS/2, FreeBSD, Mac OS X, DOS and several other platforms. Turbo51 − It is a free Pascal compiler for the 8051 family of microcontrollers, with. Now getting freepascal and Lazarus on the Pi is easy but you get quite old versions: sudo su. Apt-get install fp-compiler. Apt-get install lazarus. On Raspbian wheezy you get Freepascal 2.6.0 and Lazarus 0.9.30.4. Version dating from 2012. Working but not quite bleeding edge. Download Free Pascal. The open-source revolution started with Linux has yielded a superb free Pascal compiler as well. This compiler has versions for the Linux, DOS, OS/2, Win32, and Amiga platforms. Free Pascal is mostly compatible with Borland Pascal in syntax, and is much more useful since you can take your code to so many different platforms. It adds support for the Pascal language and its dialects like Delphi and FreePascal. Here are some of the features that Pascal provides: Syntax highlighting for files, forms and projects. A huge set of Snippets. Support for different Code Formatters (via Pascal Formatter) Source code navigation.

Online Pascal Compiler. Code, Compile, Run and Debug Pascal program online. Write your code in this editor and press 'Run' button to execute it.

Linux

FPC Binary Packages

Note: You have a number of way to install FPC presented here, which one is best for you depends very much on what you are planning to do and if you need a very current version. If you are also planning on installing Lazarus, perhaps a quick look at Installing_Lazarus and Installing_Lazarus_on_Linux is a very good idea.

With the recent release of FPC 3.2.0, only a few distributions will have the new FPC compiler in its repositories. You can do one of -

  • If you use rpm packages, you can find FPC320 in https://sourceforge.net/projects/freepascal/files/Linux/3.2.0/ or more generally in https://www.freepascal.org/download.html You might install using a command like -
  • If you use deb packages try the ones made available by, but not bundled with Lazarus at https://sourceforge.net/projects/lazarus/files/. Install using gdebi or, perhaps this command (note the ./ in front of filename, if you don't use an explicit path to filename, apt will search its own official repo, not what you want)-

FPC Tar Balls

Another option is installing from a tar, an easy and possibly even more useful model. These tars are available for a wide range of Unix like systems. You need to download the appropriate binary tar for your OS and CPU and you may also need the source files, common for all OS/CPU. You install FPC this way in your own space, not as root.

Here is a series of commands, not a script, that will install FPC on a 64 bit Linux system. It could be scripted but would would need sanity and error checking. Note that I like to install things like this in my $HOME/bin directory, if you prefer having it in $HOME, it is even easier, I am sure you can see the differences.

Note: A tar install does not resolve dependencies, if they are not already present, first install binutils, make, gcc.

Note: The second wget above assumes you are working with an amd64 (AMD or Intel) type machine, other platforms require a different tar ball. For example, for a Raspberry Pi, you would use -

which has the armhf 32bit compiler. Incidentally, the Raspberry Pi may need you to increase swap space to at least a Gig if you plan to later build any large applications with FPC.

FPC sources

The FPC source files are stored in a Subversion (SVN) repository (https://svn.freepascal.org/svn/fpc/trunk) and mirrored in a Git repository (git://github.com/graemeg/freepascal.git). The repository keeps track of all the changes by developers in the source tree. Once you have the sources, please see Installing from source under BSD/Linux for instructions on how to install them.

Update from the source repository using SVN or Git

The SVN or Git repository has been made accessible for everyone, with read-only access. This means that you can directly access the code, and you will have really the last-minute sources available. It is also a method which requires less bandwidth once you have done the first download (checkout in SVN lingo; clone in Git lingo).

Getting the source

First, you need to have an SVN or Git client installed. Use your package manager, install a tool like TortoiseSVN on Windows, or look at the Subversion web site or the Git webs site for more details and client downloads.

How To Use Free Pascal Compiler

Using command line svn: change directory (cd) to the parent directory of your development area, eg To retrieve the full source repository for the first time into an fpc subdirectory under your home directory, type:

Note: FreeBSD includes the svnlite client in the base system which you can use instead of installing the full Subversion svn client.

Using command line git: change directory (cd) to the parent directory of your development area, eg To retrieve the full source repository for the first time into an fpc subdirectory under your home directory, type:

To update the sources which were downloaded (checked out/cloned) above:

Using svn:

Using git:

Getting a separate branch

If the current trunk version is in a state of rapid change and unsuitable for much use unless you want to work on the compiler itself, you can stay on a version that is updated with fixes. To do this, you have to find out a stable branch that you want to track instead of the default trunk development version.

The example below shows how you can track the fixes_3_2 version; of course replace as needed depending on what branch you want to track.

This example keeps the fixes in another directory under your home directory - it wouldn't make sense to put two versions of the source in one directory..

Using svn:

Update as usual:

Note: There is no Git repository for fixes_3_2.

Documentation sources

The documentation sources are in a separate repository called fpcdocs, so the command to get them is:

If you want to learn more about subversion, read this excellent Subversion book which is also available online in different formats for free.

For more information, see the Free Pascal website.

Compiling the FPC source

  • First install the release FPC from rpm/deb or tar balls as described above.
  • Get FPC sources (trunk) as described above.
  • Make FPC sources with (execute in the source (trunk) directory)

Pascal Compiler For Windows 10

Decide about install/to/directory and also add options that you need.

  • Check your ~/.fpc.cfg or /etc/fpc.cfg. It either has to be valid for the trunk compiler as well or you have to move it to the ./etc directory above your compiler executable. E.g. if your compiler is in ~/software/fpc/3.2.0/lib/fpc/3.2.0 (~/software/fpc/3.2.0/bin), you have to move the fpc.cfg to ~/software/fpc/3.2.0/lib/fpc/etc.
  • If you have no valid global fpc.cfg file, you have to create one in install/to/directory/lib/fpc/etc. For this run the following commands

FreeBSD

Note: If you wish to also install Lazarus, you can omit installing FPC with the steps below as the Lazarus port will install it for you. See Installing Lazarus on FreeBSD.

Installing from the ports collection

The FreeBSD ports collection has FPC v3.2.0 version in /usr/ports/lang/fpc. FPC is scattered over 105(!) packages. The FPC source is now installed by default; it previously needed to be copied and uncompressed from /usr/ports/distfiles/freepascal.

This must be done as root.

Once FPC is installed you can check if it's working by simply running as a normal user:

which should produce output similar to this:

To fix a linking issue (as of FreeBSD 12.2, 29 Dec 2020), you must also install the GNU devel/binutils if not already installed and then use a symbolic-link from /usr/local/bin/ld.bfd to /bin/ld.bfd.

Installing from tar

Select the appropriate 32 bit/64 bit tar file based on your system. For x86_64 64-bit FreeBSD, download from x86_64. For i386 32-bit FreeBSD, download from i386.

To install Free Pascal from a terminal:

Replace the desired Free Pascal version (3.0.4, 3.2.0 or 3.3.1) and architecture (x86_64 or i386) as required.

If you want to install the Free Pascal Compiler globally, for example in /usr/local, run the install.sh script as root. install.sh script uses bash shell syntax. If you get Bad substitution error, make sure you run script with bash.

If you are on FreeBSD 12 or newer, the default linker is the lld. This may cause problems if you have code that uses, for example, the cthreads unit as in the following program:

The executable generated for this program will cause a segmentation fault when run. To fix this issue, you need to install the GNU linker:

and add a symbolic-link from /usr/local/bin/ld.bfd to /bin/ld.bfd.

OpenBSD

Installing from tar

Select the appropriate 32 bit/64 bit tar file based on your system. For x86_64 64-bit OpenBSD, download from x86_64. For i386 32-bit OpenBSD, download from i386.

To install Free Pascal from a terminal:

Replace the desired Free Pascal version (3.0.4, 3.2.0 or 3.3.1) and architecture (x86_64 or i386) as required.

If you want to install the Free Pascal Compiler globally, for example in /usr/local, run the install.sh script as root. install.sh script uses bash shell syntax. If you get Bad subtitution error, make sure you run script with bash.OpenBSD uses ksh as default shell. You need to install bash manually.

Installing from source on BSD/Linux

How To Install Free Pascal Compiler Online

Effectively, you need:

1. A file with all FPC sources (https://sourceforge.net/projects/freepascal/files/Source/3.2.0/fpc-3.2.0.source.tar.gz).

2. A starting (bootstrap) FPC compiler. An FPC release can always be built by the previously released FPC version, and FPC trunk can always be built by the current FPC release. You can download a bootstrap Free Pascal Compiler or use your distribution's package management/software system to install one.

FPC build process:

  • Fetch necessary files (starting compiler), FPC source file or source svn directory
  • If using FPC source files: extract/de-tgz in work directory,
  • Build: enter work/fpc/ and run:
  • Install FPC. Again in work/fpc, run
  • Create a symlink:
  • Install sources:
  • Create a symlink for default FPC source path:
  • Set up fpc.cfg configuration file:
  • Optionally test to see if ppcx64 -i (or whatever compiler your architecture uses) gives output, else give a warning that user needs to add $PREFIX/bin to the current path. Try to compile a program with ppcx64 -viwn, and see if that gives errors.

Notes:

  • If you need fpcmake package lists, you need to generate or supply them yourself, (in the port, or in an extra archive) either way, do a dummy install to /tmp/pack and determine the files installed with
  • $THEPREFIX and $ETCDIR should be user configurable. Otherwise local installs aren't possible.
  • BSDHIER=1 on all make commands forces BSD hierarchy conventions.

Windows

Installing an FPC release with the Windows installer

Warning: If you are intending to also install Lazarus using the official Windows installer which installs both FPC + Lazarus, please ignore these instructions and instead refer to Installing Lazarus on Windows to avoid having to resolve any resulting conflicts.

By far the easiest way to get a working installation of the Free Pascal Compiler is to download and run the appropriate self-extracting Windows installer from the official SourceForge repository for FPC 3.2.0 - this release contains the current versions of the Free Pascal Compiler and the Free Pascal libraries. The Windows installer packages install very easily, and should work 'out-of-the-box'. You will be taken through a typical Windows installation wizard to install the FPC binaries and Free Pascal libraries:

  • Choose an installation location for FPC (suggested C:FPC3.2.0)
  • Choose installation type (recommended Full Installation)
  • Choose file associations, configuration file creation etc
  • Install
  • View README

Installing from source

Installing from source with a command line Subversion (svn) client is not for novices, since you also need a working bootstrap compiler to be able to compile the Free Pascal Compiler from source. Instructions for obtaining a bootstrap compiler are included below.

Obtaining the source with a command line svn client

The easiest way to get the Free Pascal Compiler source is by using a Subversion (svn) client. The exact commands may vary between svn clients; the commands given below are for the command line Subversion client downloadable from SourceForge - win32svn.

How To Install Free Pascal Compiler
Source for a release version of FPC

First create a directory in which you'd like to keep the source. Any normal user can do this. Create a directory for FPC (eg C:Source) by typing the following at a command prompt:

Now type issue the following command at a command prompt:

Wait while the Subversion repository is downloaded to the fpc-3.2.0 directory in your newly created Source directory. This can take a while.. have a look at the helpful Free Pascal Forums while you wait :-) You may also like to read the excellent How to use the Forum Wiki article which explains how to get answers to pesky questions quickly. New Forum users are always welcome.

Source for a non-release version of FPC

There are two current non-release branches of the Free Pascal Compiler: the development (trunk) branch and the Fixes 3.2.0 branch which includes additional fixes to the released 3.2.0 version. Developers, and those who like living on the bleeding edge and testing new features and fixes, will choose the development version; more normal users, who wish to use a stable branch with some additional fixes since the latest release version, will choose the Fixes branch. The instructions below cover both these branches.

If it does not already exist, create a directory in which you'd like to keep the FPC source (eg C:Source). Any normal user can do this. Now type the following command at a command prompt:

How To Install Free Pascal Compiler

For the Fixes branch of FPC 3.2.0

For the Development (trunk) branch of FPC

Hint: To update your local copy of the repository with subsequent changes, you need only do:

For the Fixes branch of FPC 3.2.0

For the Development (trunk) branch of FPC

Obtaining the source with the GUI TortoiseSVN client

The first thing you will probably need to do is download the GUI TortoiseSVN Subversion client from TortoiseSVN website Download file area if you do not already have it available.

After downloading the appropriate Windows installer and installing it, you are ready to begin. You will notice that TortoiseSVN has added a number of shortcuts to the File Explorer context menu so that you can work easily with svn without needing to use the command prompt.

Source for a release version of FPC

To download the latest release version Free Pascal Compiler source from the Subversion repository, first create the directory in which you'd like to keep the source. Any normal user can do this:

  • Open File Explorer and navigate to 'Local Disk C:'
  • Choose Home > New folder in File Explorer and create a directory for the FPC source (eg C:Source).
  • Right click in File Explorer in your newly created directory and you should see an entry for 'SVN Checkout..' in the context menu, choose this entry and you will be presented with a TortoiseSVN Checkout Dialog.
  • Fill in the 'URL of the Repository' field with this URL: https://svn.freepascal.org/svn/fpc/tags/release_3_2_0
  • In the 'Checkout directory' field add fpc-3.2.0 to the existing C:Source in the field so you end up with C:Sourcefpc-3.2.0. This is the directory which will house your local copy of the source repository.
  • Leave the other fields at their default and click OK.
  • Now you will be presented with a new dialog which shows the progress of the checkout.. wait while the Subversion repository is downloaded to the fpc-3.2.0 directory in your newly created Source directory. This can take a while.. have a look at the helpful Free Pascal Forums while you wait :-) You may also like to read the excellent How to use the Forum Wiki article which explains how to get answers to pesky questions quickly. New Forum users are always welcome.
Source for a non-release version of FPC

There are two current non-release branches of the Free Pascal Compiler: the development (trunk) branch and the Fixes 3.2.0 branch which includes additional fixes to the released 3.2.0 version. Developers, and those who like living on the bleeding edge and testing new features and fixes, will choose the development version; more normal users, who wish to use a stable branch with some additional fixes since the latest release version, will choose the Fixes branch. The instructions below cover both these branches.

The instructions are the same as for a release version of the Free Pascal Compiler given above, except that you will need to replace the 'URL of the Repository' and 'Checkout directory' fields with the appropriate values set out below:

For the Fixes branch of FPC

For the Development (trunk) branch of FPC

Updating your local repository

To update your local copy of the repository with subsequent source changes:

  • Open File Explorer, navigate to your C:Source and right-click on the fpc-fixes-3.2.0 or fpc-trunk directory.
  • You should see an entry for 'TortoiseSVN', if you hover over it, another longer context menu will be presented from which you should choose 'Clean up..'. In the Cleanup dialog, check both 'Delete unversioned files and folders' and 'Delete ignored files and folders'.
  • Now right-click on the fpc-fixes-3.2.0 or fpc-trunk directory again and you should see an entry for 'SVN Update' in the context menu, choose this entry and you will be presented with a TortoiseSVN Update Dialog and the update will start without you needing to do anything else. When the update finishes, click OK to exit.

Obtaining a bootstrap compiler

To obtain a bootstrap Free Pascal Compiler, download the distribution package fpc-3.2.0.i386-win32.exe and run it - it is a self-extracting installer, so just follow the instructions to install it. The installer should set the PATH environment variable as appropriate.

Then restart windows.

Compiling the FPC source

Before you start

  • The installation PREFIX in the instructions that follow is totally dependent on the directory in which you installed FPC. The Windows FPC installer uses a default location of C:FPC and the FPC 3.2.0 release files are placed in C:FPC3.2.0. Since versions change relatively frequently, it is recommended that you just select and maintain a single PREFIX default location without any regard to FPC version numbers. A reasonable PREFIX to adopt is the one used by the Windows FPC installer (C:FPC) but you must also make sure that the C:FPCbini386-win32 directory is added to your path environment variable.
  • Check that the make.exe which is found first in your path environment variable is the GNU one included with your bootstrap FPC compiler. If you have a recent version of Delphi installed, watch out for its version of make being earlier in your path. It does not understand the FPC Makefile and you will receive many errors if you try to use it. You can check this by opening a command prompt and typing make -v. The result should be (or very similar):
  • If you need to change your path so that the correct make.exe is found, you can either do it temporarily or permanently.
    • To change it temporarily (it is in effect only until you close the current command prompt) to find make.exe in C:FPC3.2.0bini386-win32, at a command prompt type:
    • To change it permanently, open Control Panel > System and choose 'Advanced system settings' under 'Related settings' towards the bottom of the page. Now choose 'Environment Variables..' in the System Properties dialog. Here you will probably need to change the Path that appears under 'System variables' so that C:FPC3.2.0bini386-win32 occurs first.

Instructions

  • At a command prompt, navigate to your local FPC source directory (eg C:Sourcefpc-3.2.0) by typing:
  • Compile the FPC source with:
  • To overwrite an existing FPC installation in, for example, C:FPC) type:
  • If you also need the cross-compiler to x86_64, type the following commands which will compile the cross-compiler and then install it:

The cross-compiler to compile programs for Windows 64 bit may now be found here: C:FPC3.2.0bini386-win32ppcrossx64.

Done!

macOS

Warning: If you are intending to also install Lazarus using the official macOS installation packages which install both FPC + Lazarus, please ignore these instructions and instead refer to Installing Lazarus on macOS to avoid having to resolve any resulting FPC conflicts.

Note: For installation on Apple Silicon/AArch64, please refer to Apple Silicon Support for build instructions for a native Free Pascal Compiler.

Installing an FPC release from the official macOS packages

Step 1: Download Xcode (optional)

Xcode is a 12GB download which will take up 16GB of disk space. You only need to download and install the full Xcode development environment if you need:

  • the SDKs for iOS, iPadOS, watchOS and tvOS; or
  • to validate and upload apps to the Mac App Store; or
  • to notarise apps for distribution outside of the Mac App Store.

Xcode 11.3.1 for use on macOS 10.14 Mojave must now be installed by downloading it from Apple Developer Connection (ADC), which requires free registration. Xcode 11.4.x for use on macOS 10.15 Catalina can be installed from the Mac App store. Note that you must first move any old Xcode versions from the Applications folder into the trash or rename the Xcode app (eg Xcode.app to Xcode_1014.app). You can select which version of Xcode to use with the command line utility xcode-select.

Older systems:

The developer tools can be installed from the original macOS installation disks or a newer copy downloaded from the Apple Developer Connection (ADC), which requires free registration. Download the Xcode file, it will end up in your Downloads directory as a zip file. Click it. It is unarchived into your Downloads directory. You may be happy with it there but maybe not. Other users will see the path to it but be unable to use it. And it is untidy there. So I moved mine and then told xcode-select where it was moved to (in a terminal) -

Step 2: Xcode Command Line Tools

This is shown here as a separate step because it really is a separate step in addition to Step 1. Don't confuse this with the internal Xcode command line tools that the Xcode GUI will tell you are already installed. FPC does not use those Xcode internal command line tools, so do the following (it is quick and easy)-

Step 3: Download FPC

Download the Free Pascal Compiler (FPC) and FPC source from the Free Pascal website. When you arrive at the download page, select a mirror and then choose the correct version for your operating system.

These installation packages are built by the FPC developers and track formal releases. As these installation packages are not code signed nor notarized by Apple, you need to either control-click on the installation package and choose 'Open' or right-click on the installation package and choose 'Open' in the contextual menu and confirm you want to install from an Unknown Developer.

Other installation options

fpcupdeluxe

You could also consider using fpcupdeluxe to install FPC.

fink & MacPorts

You could also consider using one of the package managers for macOS, fink or MacPorts, to install FPC. Their extra bonus is easy installation as well as clean removal of FPC and a huge number of other open source software packages. Fink also offers numerous Free Pascal crosscompilers for many processors and operating systems.

Installing from source

There are two current non-release branches of the Free Pascal Compiler: the development (trunk) branch and the Fixes 3.2.0 branch which includes additional fixes to the released 3.2.0 version. Developers, and those who like living on the bleeding edge and testing new features and fixes, will choose the development version; more normal users, who wish to use a stable branch with some additional fixes since the latest release version, will choose the Fixes branch. The instructions below cover both these branches.

The source code is kept in a version control system called subversion or svn for short, and is mirrored in git:

  • macOS 10.5 and higher already contain a command line svn or git client if you have installed the Xcode command line utilities. You can also use fink, port or brew to get an svn client on newer macOS. SVN clients with GUI (graphical user interface) are available from Versiontracker. A quite handy client, which integrates in Finder, is SCPlugin.
  • You also need the latest released Free Pascal Compiler version (3.2.0 as of January 2021) installed in order to be able to successfully compile the development (trunk) version.

FPC Trunk

  • FPC User Changes in Trunk - may break existing code.

Note that since FPC trunk is by definition still under development, some of the features may still change before they end up in a release version.

Create a directory where you would like to put the source (eg fpc in your home directory). You don't need to be root to do this. Any normal user can do this. Open an Applications > Utilities > Terminal and do the following:

Using git:

Using svn:

This will create a directory called 'fpc' and download the FPC source to it.

To subsequently update your local source repository with the latest source changes you can simply do:

Using git:

Using svn:

To build and install FPC (the highlighted text should be all on one line):

You will also need to update the links for the compiler in /usr/local/bin which will be pointing to the previous FPC version. For example:

Note that you will need to build a new ppc386 compiler if you want to continue to compile 32 bit applications by replacing these lines (this may not be possible after Xcode 11.3.1 and macOS 10.14.6 Mojave because of Apple's removal of 32 bit frameworks):

with these two lines:

FPC Fixes 3.2

Create a directory where you would like to put the source (eg fpc-fixes-3.2 in your home directory). You don't need to be root to do this. Any normal user can do this. Open an Applications > Utilities > Terminal and do the following:

This will create a directory called fpc-fixes-3.2 and download the FPC source to it.

To subsequently update your local copy of the repository source with the latest source changes you can simply do:

To build and install FPC (the highlighted text should be all on one line):

You will also need to update the links for the compiler in /usr/local/bin which will be pointing to the previous FPC version. For example:

Note that you will need to build a new ppc386 compiler if you want to continue to compile 32 bit applications by replacing these lines (this may not be possible after Xcode 11.3.1 and macOS 10.14.6 Mojave because of Apple's removal of 32 bit frameworks):

Testing the FPC Install

You might like to try a simple and quick test of FPC at this stage. From the command line (Mac - Open an Application > Utilities > Terminal) and copy this very short program and save it as the file helloworld.pas:

Now compile this simple code by typing this command and then pressing Enter:

It should very quickly make an executable called, you guessed it, 'helloworld'. Run this executable by typing this command and then pressing Enter:

If that worked, well done!

Useful command-line switches for compiling from source

  • Significantly speed up the FPC source tree compilation
    • FPMAKEOPT='-T x' (x = the number of CPU cores you have)
  • Reduce the number of times the compiler itself gets recompiled
    • NOWPOCYCLE=1
  • Add debug information and disable optimizations during compilation of the compiler/RTL
    • OPT='-gl -O-'

Installing cross compilers

A cross compiler allows you to create binaries (executables) for a platform different from the platform being used for compilation. For example, working under macOS and creating executables for Win32, FreeBSD or Linux. For details on how to do this, see Cross Compiling.

See also

  • FPC Wiki Documentation.
  • FPC Official Documentation.
  • FPC 3.2.0 New Features.
  • FPC 3.2.0 User Changes - may break existing code.
  • FPC New Features in Trunk.
  • FPC User Changes in Trunk - may break existing code.
Retrieved from 'https://wiki.lazarus.freepascal.org/index.php?title=Installing_the_Free_Pascal_Compiler&oldid=144785'

Deutsch (de)English (en)français (fr)magyar (hu)

  • 4Step 1: Compiler or IDE or both
    • 4.3Search strategies
  • 7Case 3: IDE finds a unit, but compiler does not

Overview

This page is about the most dreaded, prominent error of Pascal: unit not found or can't find unit.

This error has a lot of potential reasons, so this page attempts to gather most of those, plus how to identify which particular problem it is and how to hopefully solve it. Usually, under this condition, Lazarus does not compile any program at all nor does it recompile the IDE any longer, meaning Lazarus is completely 'broken' until the fix is applied.

Note: Lazarus provides a tool to test for many common misconfigurations: The button Project Options / Test. Click these and see the warnings it produces. Normally it will only warn (safely ignore those) about certain duplicate ppu files in the fpc sources (the actual file dupes depend on the fpc version and distribution).

Quick list

For experts here is a list of possible causes for the error. This list is not sorted for probability.

  • FPC installation
    • You named one of your units like one of the FPC units.
    • Wrong fpc.cfg. $HOME/.fpc.cfg instead of /etc/fpc.cfg. Find out with -vut.
    • Missing or wrong search path in fpc.cfg. Find out with -vut.
      • Added a search path to fpc.cfg that contains sources (.pas,.pp)
    • Cross compiling: Forgot to install the cross compiled FPC ppus. Find out with -vut.
    • Broken FPC installation. Remove FPC directory manually and install again.
    • Compiler symlink points to wrong compiler.
      • subcase: 2 compilers in searchpath, see: install on Linux cheatsheet
    • FPCDIR environment variable is set
    • You named one of your include files like one of the FPC include files and your FPC installation was not compiled with -Ur.
    • Specific problem of Ubuntu installations
  • Lazarus
    • Forgot to add a package dependency. Check project inspector or package editor.
    • A ppu exists twice. Find out with -vut. Remove the wrong ppu. Check if a package usage unit paths is only $(PkgOutDir) and the usage include path is empty. Check if two packages or a package and the project contain the same unit search path in their compiler options.
    • Typo or missing search paths. Check Project / Project Options / Compiler Options, pages Paths and Inherited and check source editor / popup menu / file settings / unit info / unit paths.
    • Wrong case. If the unit filename uses upper and lower case letters all uses sections must use the correct case. Before 0.9.29 the IDE did not search 'in' filenames in lower and upper case. Check that the 'in' filenames in uses sections.
    • Wrong unit: If there are two units with the same name in the search path the compiler may use the wrong one without warning. The IDE normally warns unless disabled in Tools / Options / Environment / Naming / ambiguous file action.
    • Forgot to install the FPC sources and/or forgot to set the path in Tools / Options / Environment / Files / FPC source directory.
    • Using the wrong version of FPC sources
    • Using the wrong compiler. Check Tools / Options / Environment / Files / compiler path. Also click 'project options / Test' to diagnose for this condition
    • Using the wrong lazarus directory. Check Tools / Options / Environment / Files / Lazarus directory.
    • Added a search path to your own units to fpc.cfg instead of creating a package.
    • Cross compiling: Forgot to cross compile lcl, synedit for the new platform. These are not compiled automatically (They are since 0.9.31).
    • Wrong package: you have a package (.lpk) twice on your disk and the IDE is still using the old

Notations, terms

  • .pas file: This is a place holder for any pascal unit source. For example unit1.pas or unit1.pp. Both extensions are allowed and there is no difference. There are other compilers, which ignore .pp files, but this page is about the Free Pascal compiler.
  • .pp file: Same as .pas. Just another extension for the same thing.
  • .p file: Same as .pas. Just another extension for the same thing. Commonly utilized on Macintosh.
  • .ppu file: This is place holder for any compiled pascal unit, created by the Free Pascal compiler.

Step 1: Compiler or IDE or both

The IDE and the compiler do not use the exact same way to locate units.

Compiler

If the compiler can not find it, the compilation fails (Build, Ctrl+F9) with the error 'unit not found'.

IDE

Turbo Pascal Compiler Download

If the IDE can not find, the codetools will fail. For example Find Declaration (Alt+Up or Ctrl+Left mouse button) or Identifier completion (Ctrl+Space) will fail with the error 'unit not found'.If the error message is about units you don't use directly, then simply add the unit name to a uses section and do a find declaration. After that you can remove it.

Search strategies

What both search

  • Both search units in the search path.
  • Both search first for pascal sources (.pas, .pp) files. Under MacPAS mode: search for .p files too.
  • Both search for several cases: normal case, lower case, upper case. For example: if the uses section contains 'Unit1', then it searches:

They are searched in every directory of the search path.

Differences between IDE and FPC unit search

  • FPC uses the fpc.cfg for additional search paths. The IDE uses them only to find files of the FPC sources. So adding a search path to the fpc.cfg is ignored by Lazarus.
  • FPC always starts in one directory and every relative filename is relative to this directory. For the Lazarus IDE each project and each package has its own base directory. So the search path of a package is relative to the package directory. The package directory is the directory of the .lpk file. The base directory of a project is the projects main source file (e.g. the .lpr file). If there is no main source file, the directory of the .lpi file is taken. Normally both are the same and the IDE has no graphical frontend to split. But by editing the .lpi file manually you can split them. At the moment there is only one prominent example for this case: the ppxxx.lpi files of the Free Pascal compiler.
  • FPC and Lazarus have different macros.
  • You can define additional source search paths for the IDE, which are not given to the compiler. (Compiler options -> Other source files)

Case 1: Both can not find a unit

  • Check if the unit exists: If not, then see chapter Searching packages, projects of a unit (ToDo: write me).
  • Check if the unit is readable: (You can open it in the IDE). A unit source does not need to be writable. If not, then fix the permissions or get a readable copy.
  • Check if the unit has a valid pascal name: (identifier plus extension). Valid examples: unit1.pas, Bogus2.pp, _1.pas, LAZ.PAS. Wrong examples: 1.pas (starts with number), a%.pas (contains special character), unit.PAS (extension uppercase, but filename lowercase).
  • Check if directory name contains special characters
  • Check for right case: If your unit name is all lowercase or all uppercase you don't need to worry. If not, then you must make sure, that every uses section in every source code uses exactly this case. For example: if your unit is named Unit1.pas, then every uses section must use Unit1. This is not so important under windows, but under any other platform (Linux, MacOSX). The IDE gives warnings when saving files with mixed case.
  • Check if the unit is in the search path. If the compilation stopped while compiling a package, look in the compiler options of that package (open the package, there is a button 'compiler options'). If the compilation stopped while compiling the project, look in the project / compiler options.

Case 2: Compiler finds a unit, but IDE does not

How To Install Free Pascal Compiler Free

The next sub cases assumes, that Run -> Build (Ctrl+F9) in the Lazarus IDE works, but Find Declaration fails with the error: unit not found.If unsure, check Case 1 as well.

  • Check if the unit source exists: The IDE searches for pascal source (.pas, .pp) and ignores the .ppu files. The compiler can use a .ppu, but the Lazarus IDE not. This feature is planned, but there is no time frame.
  • Check if unit directory was added to your fpc.cfg: The compiler uses all paths defined in the fpc.cfg file. The IDE does not read this file. Add the search path to the compiler options of your projects and/or packages.
  • Check for macros and special compiler flags: The compiler understands the asterisk * character in search paths. The IDE treats this character as normal character. You can add the -Fu, -FU, -FE flags manually to the custom compiler flags in the lazarus IDE, but they are not parsed. Always use the right fields for options. The custom options are only for flags like the -D flag or uncommon compiler options, like new features of the unstable developer compiler.
  • Rescan the FPC source directory: sometimes after a new/fresh install the IDE has problems finding the FPC sources. Selecting this option from the main menu (Environment/Rescan the FPC directory) might fix the problem.

Case 3: IDE finds a unit, but compiler does not

This is the case, when Find Declaration finds the unit, but building (Ctrl+F9) fails.

There are two cases:Either the compiler didn't find anything, or it does not like, what it found. In both cases it returns the same error: unit xyz not found.

You can see where the compiler sarches and what it finds by adding the -vu -vt options (short: -vut). Beware: This gives easily thousands of lines of output.The -vut must be applied to the step, that creates the error. If it happens when compiling the project, enable the -vt in the compiler options of the project. If it happens when building a package, use the compiler options of the package. If it happens when building the IDE, use the Tools / Configure Build Lazarus / Advanced Build Options / Options.Then compile again. It will slow down the compilation.Then right click on on messages window and click on 'Copy all and hidden messages to clipboard'. Then paste it i the source editor. Now search for all places where the unit name is mentioned.

Case 3.1: The compiler finds the .ppu file, but does not like it

This can happen, when the found .ppu file is older than a required unit (.pas, .pp or .ppu). For example if unit1 uses unit2 and unit2 was changed, then unit1 needs to be recompiled as well.

It can also happen, if you installed newer files without deleting the old ones. The compiler stops at the first found.

You can see, what the compiler finds and what is missing by giving the -vt command line switch. But this gives a lot of output and it is hard to read for newbies.

The easiest way is often to first recompile the packages and project clean (Build all). In case of the Lazarus sources itself: The configure build lazarus dialog has a button to setup all options for a clean build.

Best Pascal Compiler

Case 3.2: The compiler cannot find source (.pas, .pp) or .ppu file

In this case you have to extend the search path. The question is: add the .ppu directory or the source directory?

Warning: If you add the wrong search path, it can happen, that the compiler will create multiple copies of files and you will later get the 'unit not found' error of Case 3.1.

Many projects and packages have distinct source directories (.pas, .pp) and output directories (.ppu). If a package has an output directory, then only this directory should be added to the search path of any depending project and/or package. The IDE will do this automatically. For example: Adding the LCL as dependency to your project adds two output directories to your unit search path. You can see this in Compiler Options -> Inherited. Never add any other LCL directory to your search paths or you will get Case 3.1 errors.

  • Check if the unit is in the compiler search path: The search path given to the compiler are the -Fu options of the command line parameters. You can see the options given to the compiler in Compiler Options -> Show Options.

Case 3.3: The wrong compiler is being used

Perhaps when cross compiling the correct cross .ppu is found, but the wrong compiler is used by lazarus (see install on Linux cheatsheet).

Pascal Online Compiler

Lazarus uses the compiler specificied in the environment options. It is easy to forget to change the compiler, if you change the target CPU. Therefore it is recommended to set the compiler in the environment options to the fpc executable, which is a front end to the ppcXXX compiler backends. The fpc executable chooses the right ppcXXX compiler based on the -P parameter or the native pccXXX compiler if no such parameter is given. The drawback of using fpc instead of for example ppc386 directly, is that an extra process needs to be started (one for fpc and one for ppcXXX) which slows down compilation slightly, but this is hardly measurable.

Run lazarus from a text console (with startlazarus) so you an see the debugging output and look at the output generated when you compile.

It may look like this:

The clues are these lines:

Which suggest in this case that ppc386 is set in the Environment menu instead of fpc


Case 3.4: make , apt or fpcUPdeluxe installed a 2nd compiler fpc into $PATH

see install on Linux cheatsheet how to move the 2nd fpc out of the $PATH . Click 'project options / Test' for diagnosis of this condition.

Retrieved from 'https://wiki.freepascal.org/index.php?title=Unit_not_found_-_How_to_find_units&oldid=140889'