Difference between revisions of "Installing the Free Pascal Compiler"

From Free Pascal wiki
Jump to navigationJump to search
m (Improve installation instruction for FreeBSD using tar)
Line 166: Line 166:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
$ FPC_VERSION=3.2.0
 
$ FPC_VERSION=3.2.0
$ wget ftp://ftp.freepascal.org/pub/fpc/dist/$FPC_VERSION/x86_64-freebsd/fpc-$FPC_VERSION.x86_64-freebsd11.tar
+
$ PLATFORM=x86_64
$ tar -xf fpc-$FPC_VERSION.x86_64-freebsd11.tar
+
$ wget ftp://ftp.freepascal.org/pub/fpc/dist/$FPC_VERSION/$PLATFORM-freebsd/fpc-$FPC_VERSION.$PLATFORM-freebsd11.tar
$ cd fpc-$FPC_VERSION.x86_64-freebsd
+
$ tar -xf fpc-$FPC_VERSION.$PLATFORM-freebsd11.tar
 +
$ cd fpc-$FPC_VERSION.$PLATFORM-freebsd
 
$ sh install.sh
 
$ sh install.sh
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Replace FPC_VERSION with desired Free Pascal version.
+
Replace FPC_VERSION with desired Free Pascal version, such as 3.2.0, 3.0.4, etc. Replace PLATFORM with desired platform, such as X86_64 or i386.  
  
 
If you want to install the Free Pascal Compiler globally, for example in /usr/local, run the install.sh script as root.
 
If you want to install the Free Pascal Compiler globally, for example in /usr/local, run the install.sh script as root.

Revision as of 10:44, 7 July 2020

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

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 is very comprehensive.

Overview

There are a number of ways to install FPC on your system. The current version and trunk and, sometimes beta and release candidates are available direct from the FPC website https://sourceforge.net/projects/freepascal/files/ (tar, exe, rpm, dmg files). In addition, versions of FPC are provided (including deb files), from the Lazarus download site, http://sourceforge.net/projects/lazarus/files/. Linux users can almost certainly find FPC in their distro's repositories but may find it is out dated.

It is possible to build FPC from source, typically each release can 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.

Linux

FPC binaries

With the recent release of FPC 3.2.0, few distributions will have the new FPC compiler in its repositories and, as yet, the FPC 3.2.0 deb bundled with a Lazarus release is not available.

However, installing from a tar is easy and possibly even more useful for some users. You need to download the appropriate binary tar for your OS and CPU and you will also (probably) 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.

Light bulb  Note: It is assumed you already have a previous FPC working and therefore don't need to resolve dependencies, if that is not the case, first install binutils, make, gcc, subversion, vim, libgtk2.0-dev, libx11-dev, and possibly libqt5pas-dev.
cd
mkdir -p bin/FPC/Tars
mkdir bin/FPC/SRC
cd bin/FPC/Tars
# download src and compiler tars, change for different CPU (or Mac ?) 
wget https://sourceforge.net/projects/freepascal/files/Source/3.2.0/fpc-3.2.0.source.tar.gz
wget https://sourceforge.net/projects/freepascal/files/Linux/3.2.0/fpc-3.2.0-x86_64-linux.tar
tar xf fpc-3.2.0-x86_64-linux.tar
cd fpc-3.2.0-x86_64-linux
./install.sh
# when asked where to install, enter $HOME/bin/FPC/fpc-3.2.0, accept all defaults after that.

cd ../../SRC
tar xzf ../Tars/fpc-3.2.0.source.tar.gz

# Set a path to where the compiler is, add line at end of .bashrc, if you don't use bash, adjust ! 
echo "PATH=\"\$HOME/bin/FPC/fpc-3.2.0/bin\":\"\$PATH\"" >> ~/.bashrc
source ~/.bashrc

# trivial test
cd 
fpc -iV

The following instructions relate to installing from a package

The latest release of Free Pascal Compiler can be obtained from the Free Pascal website (then choose a mirror) or from SourceForge.

At the Lazarus download file area on sourtceforge you can get the RPM or DEB of the Free Pascal compiler and its packages. If you don't have an RPM-based or Debian-based distribution, you can download and install the tarballs from the Free Pascal website. If you want to compile the binaries for yourself, see the From source on Linux/BSD section.

Instructions:

Warning-icon.png

Warning: If you're not using RPMs or Debian packages (even if you plan to use alien) it's best to get latest stable FPC.

Start Linux and login as root.

Download latest FPC deb or rpm files from SourceForge.

As an example:

  • fpc-3.0.4-1.x86_64.rpm
  • fpc-src-3.0.4-1.x86_64.rpm

and install them with:

  • rpm -Uvh *.rpm

Debian users are recommended to use the deb packages, but may use either alien (warning, it doesn't generate fpc.cfg) or the tarball install.

FPC sources

The FPC source files are stored in a Subversion (SVN) repository that keeps track of all changes of the source tree. Once you have the sources, please see installing Free Pascal from source under Linux/BSD for instructions on how to install them.

Update from the source repository using SVN

The SVN 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).

Getting the source

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

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:

cd ~
svn checkout http://svn.freepascal.org/svn/fpc/trunk fpc

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

cd ~
svn update fpc

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_2_6 version; of course replace as needed depending on what branches 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...

cd ~
svn checkout http://svn.freepascal.org/svn/fpc/branches/fixes_2_6 fpc_fixes_2_6

Update as usual:

cd ~
svn update fpc_fixes_2_6

Documentation sources

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

cd ~
svn checkout http://svn.freepascal.org/svn/fpcdocs/trunk fpcdocs

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.

FreeBSD

Light bulb  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

The FreeBSD ports collection has FPC v3.0.4 version in /usr/ports/lang/fpc. FPC is scattered over 38 (!) 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.

# cd /usr/ports/lang/fpc && make install && make clean

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

$ fpc test

which should produce output similar to this:

 Free Pascal Compiler version 3.0.4 [2019/07/21] for x86_64
 Copyright (c) 1993-2017 by Florian Klaempfl and others
 Target OS: FreeBSD for x86-64
 Compiling test
 Fatal: Cannot open file "test"
 Fatal: Compilation aborted
 Error: /usr/local/bin/ppcx64 returned an error exitcode

From tar on FreeBSD

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:

$ FPC_VERSION=3.2.0
$ PLATFORM=x86_64
$ wget ftp://ftp.freepascal.org/pub/fpc/dist/$FPC_VERSION/$PLATFORM-freebsd/fpc-$FPC_VERSION.$PLATFORM-freebsd11.tar
$ tar -xf fpc-$FPC_VERSION.$PLATFORM-freebsd11.tar
$ cd fpc-$FPC_VERSION.$PLATFORM-freebsd
$ sh install.sh

Replace FPC_VERSION with desired Free Pascal version, such as 3.2.0, 3.0.4, etc. Replace PLATFORM with desired platform, such as X86_64 or i386.

If you want to install the Free Pascal Compiler globally, for example in /usr/local, run the install.sh script as root.

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

uses cthreads;
begin
    writeln('hello'); 
end.

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

root# pkg install binutils

and make sure that /usr/local/bin/ occurs in your PATH before /usr/bin/.

From source on Linux/BSD

Effectively, you need:

If you have a file with all FPC sources, or two (FPC and Lazarus):

1. For fpc ftp://ftp.freepascal.org/fpc/dist/Linux/separate/sources.tar preferably an export (no SVN/dirs).

2. Lazarus source snapshot.

3. 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:
# Linux use:   
export MAKE=`which make` ; echo $MAKE 
# FreeBSD use (default csh, or tcsh):
set MAKE=`which gmake` ; echo $MAKE
# FreeBSD use (bash):
export MAKE=`which gmake` ; echo $MAKE
$MAKE all OPT='-gl' PP=/path/to/startingcompiler-name-ppc386
# $MAKE is make on Linux and gmake on BSD 
# /path/to/ can be omitted when ppc386 is in the path
  • Install fpc. Again in work/fpc, run
$MAKE install PP=compiler/ppc386 PREFIX=$THEPREFIX
#replace the PP=compiler/ppc386 with the relevant compiler if not on Intel x86
#THEPREFIX= usually is /usr/local or just /usr, but e.g. on NetBSD it is /usr/pkg for ports)
  • Create a symlink:
ln -s $THEPREFIX/lib/fpc/3.0.0/ppc386 $THEPREFIX/bin/ppc386
  • Install sources:
$MAKE install sourceinstall PREFIX=$THEPREFIX
  • Create a symlink for default Lazarus source path:
ln -sf $THEPREFIX/share/src/3.0.0/fpc /usr/share/fpcsrc
  • Set up fpc.cfg configuration file:
$THEPREFIX/lib/fpc/3.0.0/samplecfg $THEPREFIX/lib/fpc/3.0.0 $ETCDIR
  • Optionally test to see if ppc386 -i (or whatever compiler your architecture uses) gives output, else give a warning that user need to add $PREFIX/bin to his current path. Try to compiler a program with -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
    find . >ll
    
  • $THEPREFIX and $ETCDIR should be user configurable. Otherwise local installs aren't possible.
  • BSDHIER=1 on all make commands forces BSD hierarchy conventions.

Windows

By far the easiest way to get a working installation of Free Pascal is to download the current binary Windows release of Lazarus from the SourceForge repository - the release contains the current versions of the Free Pascal Compiler and the Free Pascal libraries as well as the Lazarus IDE.

From source on Windows

You can get the installer zip for fpc at Free Pascal's download section (http://www.freepascal.org/download.html, then choose a mirror).

Installing from the sources -- see the next section to know how to get them -- is not for novices, since you need a starting compiler as well.

FPC Sources for Windows

<<<< See section above under FPC Sources for Linux, where the use of SVN is described >>>>

The easiest way to get the Free Pascal sources is via SVN; see the next section for more on that. You can also download the package as a whole -- see http://www.freepascal.org/develop.html for the daily snapshot of the release tree.

Windows FPC Sources via SVN

You will need to have a SVN client such as TortoiseSVN installed in order to perform the steps below. The exact commands vary between SVN clients; the ones given below are to be used under SVN home's client, which is available for download here.

First create a directory in which you'd like to put the sources. Any normal user can do this. Create a directory for fpc (e.g. C:\Source), then do the following at the command prompt:

C:\Source> svn co http://svn.freepascal.org/svn/fpc/trunk fpc

Hint: To download/update the latest changes you can simply do

C:\> cd Source\FPC
C:\Source\FPC> svn up

See: http://www.freepascal.org/down/i386/win32.html . Download FPC as one big file, unzip it and run the install.exe.

Extending your PATH variable to the fpc directory:

  • Win98: Edit autoexec.bat and add the line: PATH=C:\pp\bin\bin\win32;%PATH% NO trailing \ !
  • WinXP/2k: My Computer (Right Click) -> Properties -> Advanced (Page) -> Environment Variables -> System Variables -> Edit "PATH", Add "C:\pp\bin\bin\win32" there.

Then restart windows.

After you have FPC binaries installed you can build FPC source from subversion.

Hints:

  • Windows (7+) requires that an elevated user status command prompt be used. From the start menu for "Command Prompt" right click and select "Run as Administrator".
  • YOUR-PREFIX is totally dependent on where you installed FPC to. At the time of this writing, the binaries are instructed to use a default location of "C:\FPC" and they were placed in "C:\FPC\2.6.4". Under Linux, the make install scripts were adjusted to create a new sub-folder IF the FPC version changed since last build. The Windows scripts do not. So if you know the sub-folder name ie. 3.1.1 you can specify that. However, since versions change frequently, it is recommended that you just select and maintain a single PREFIX with no respect for FPC versions. A good prefix is C:\FPC but you must also make sure that the C:\FPC\bin\i386-win32\ folder is added to your path environment variable (see above on how to set your path and change it from the binary version to the newly compiled one).

Instructions:

  • In command Prompt navigate to the localized FPC source. ie.) type "cd c:\Developer\FPC"
  • To build FPC type "make all"
  • To overwrite existing FPC type "make install PREFIX=YOUR-PREFIX"
  • To install source type "make install sourceinstall PREFIX=YOUR-PREFIX"

Compiling/installing FPC from sources with SVN (Win32)

Version FPC 3.0.4 or trunk

STEP #1: Create directories and get the sources

Create the following directories:

 c:\freepascal\
 c:\freepascal\binutils\
 c:\freepascal\binutils\i386-win32\
 c:\freepascal\fpc\
 c:\freepascal\fpc\3.0.4\

or for fpc trunk:

 c:\freepascal\
 c:\freepascal\binutils\
 c:\freepascal\binutils\i386-win32\
 c:\freepascal\fpc\
 c:\freepascal\fpc\trunk\

You will need the latest 'released compiler to build a new compiler. Get the ppc386 (the compiler) with FTP (below) and unzip it in c:\freepascal\binutils\

 ftp://ftp.freepascal.org/pub/fpc/dist/3.0.4/bootstrap/i386-win32-ppc386.zip

After installing TortoiseSVN, download the sources from SVN using a URL for each directory, see:

 Dir: c:\freepascal\binutils\i386-win32\
 URL: http://svn.freepascal.org/svn/fpcbuild/branches/fixes_3_0/install/binw32

or for fpc trunk:

 Dir: c:\freepascal\binutils\i386-win32\
 URL: http://svn.freepascal.org/svn/fpcbuild/trunk/install/binw32
 Dir: c:\freepascal\fpc\3.0.4
 URL: http://svn.freepascal.org/svn/fpc/branches/fixes_3_0/

or for fpc trunk:

 Dir: c:\freepascal\fpc\trunk
 URL: http://svn.freepascal.org/svn/fpc/trunk/

STEP #2: Create a BAT file to compile FPC

After everything is downloaded, we need a BAT file to compile the FPC sources. Create a new file c:\freepascal\makefpc.bat and copy/paste the following script:

@echo on
set myroot=c:\freepascal
set myFPC=%myroot%\fpc\3.0.4 
set mybinutils=%myroot%\binutils
set PATH=%PATH%;%mybinutils%\i386-win32;%myFPC%\bin\i386-win32
cd %myFPC%
rd /s /q  %myfpc%\examples
svn cleanup . --remove-unversioned  --remove-ignored
make distclean all install INSTALL_PREFIX=%myFPC% PP=%mybinutils%\ppc386.exe DATA2INC=%myFPC%\utils\data2inc.exe
cd /d %myFPC%\bin\i386-win32
fpcmkcfg -d basepath=%myFPC% -o .\fpc.cfg 
copy fpc.exe %mybinutils%\i386-win32

or for fpc trunk:

@echo on
set myroot=c:\freepascal
set myFPC=%myroot%\fpc\trunk 
set mybinutils=%myroot%\binutils
set PATH=%PATH%;%mybinutils%\i386-win32;%myFPC%\bin\i386-win32
cd %myFPC%
rd /s /q  %myfpc%\examples
svn cleanup . --remove-unversioned  --remove-ignored
make distclean all install INSTALL_PREFIX=%myFPC% PP=%mybinutils%\ppc386.exe DATA2INC=%myFPC%\utils\data2inc.exe
cd /d %myFPC%\bin\i386-win32
del fpc.cfg
fpcmkcfg -d basepath=%myFPC% -o .\fpc.cfg 
copy fpc.exe %mybinutils%\i386-win32

For crosscompiler to x86_64 add the following after the first make:

make all OS_TARGET=win64 CPU_TARGET=x86_64 INSTALL_PREFIX=%myFPC% PP=%mybinutils%\ppc386.exe DATA2INC=%myFPC%\utils\data2inc.exe
make crossinstall OS_TARGET=win64 CPU_TARGET=x86_64 INSTALL_PREFIX=%myFPC% PP=%mybinutils%\ppc386.exe DATA2INC=%myFPC%\utils\data2inc.exe

STEP #3: Make and install FPC

At the prompt (cmd.exe), navigate to the directory c:\freepascal and execute the script we just wrote:

cd /d c:\freepascal
makefpc.bat

Done!

macOS

Warning-icon.png

Warning: If you are intending to also install Lazarus, please ignore these instructions and instead refer to Installing Lazarus on macOS.

Step 1: Download Xcode

You need the Apple Developer tools, which are a part of the Xcode development environment.

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) -

mv Downloads/Xcode.app /Developer/.
sudo xcode-select -s /Developer/Xcode.app/Contents/Developer

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 cannot use those Xcode internal command line tools, so do the following (it is quick and easy)-

sudo xcode-select --install
sudo xcodebuild -license accept

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 binary install kits are built by the FPC developers and track formal releases. As these install kits 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 macOS installation options

fpcupdeluxe

You could also consider using fpcupdeluxe to install FPC.

fink

Alternatively, you can use fink, a package manager for macOS to install FPC. The extra bonus of fink is easy installation as well as clean removal of a huge number of other open source software packages, including Free Pascal crosscompilers for many processors and systems.

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:

program helloworld;
begin
	writeln('hello world !');
end.

Now compile this simple code with this command line:

fpc helloworld.pas <enter>

It should very quickly make an executable called, you guessed it, "helloworld". Run this executable with this command:

./helloworld <enter>

If that worked, well done!

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