Installing the Free Pascal Compiler/zh CN

From Lazarus wiki
Revision as of 08:21, 14 August 2020 by Robsean (talk | contribs) (→‎概述)
Jump to navigationJump to search

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

这里有一个如何安装和构建FreePascal编译器的广泛讨论,网址:http://www.stack.nl/~marcov/buildfaq.pdf - 对于一些用户来说,它可能有点太过于详细了,但是它是非常全面的。

概述

这里有很多方法来在你的系统上安装FPC。当前发布版本,trunk版本,有时beta版本和RC版本也可以从FPC网站https://sourceforge.net/projects/freepascal/files/ (tar, exe, rpm, dmg 文件)直接获得。另外,从Lazarus的下载网站中也提供了FPC的版本(包括rpm和deb文件),Lazarus网址http://sourceforge.net/projects/lazarus/files/。 Linux用户差不多都能从Linux发行版的存储库中找到FPC,但是可能会发现FPC是过时的版本。

从源文件构建FPC也是可能的,通常每个发布版本都可以使用先前的发布版本构建。在系统安装应用程序空间中(通常需root几或administrator访问权限)或在你自己的用户控件中安装FPC都是能做到的。这些取决于您的特定的操作系统环境。

Linux

FPC二进制文件软件包

尽管最新的FPC3.2.0已经发布,但是只有少数发行版的存储库中存在新的FPC编译器。你可以做一个FPC -

  • 如果你使用rpm软件包,你可以在https://sourceforge.net/projects/freepascal/files/Linux/3.2.0/中找到FPC320,或者在https://www.freepascal.org/download.html中的更大范围内查找,你可以使用一个命令来安装,像 -
yum localinstall fpc-3.2.0-1.x86_64.rpm
// 或者
rpm -Uvh fpc-3.2.0-1.x86_64.rpm
  • 如果你使用deb软件包, 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)-
    apt install ./fpc-laz_3.2.0-1_amd64.deb
    

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.

Light bulb  Note: A tar install does not resolve dependencies, if they are not already present, first install binutils, make, gcc.
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

# 在.bashrc的结尾处,添加并设置一个编译器在哪里的路径,如果你不使用bash,相应地调整! 
echo "PATH=\"\$HOME/bin/FPC/fpc-3.2.0/bin\":\"\$PATH\"" >> ~/.bashrc
source ~/.bashrc

# 不重要的测试
cd 
fpc -iV
Light bulb  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 -
wget https://sourceforge.net/projects/freepascal/files/Linux/3.2.0/fpc-3.2.0.arm-linux.tar
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源文件

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 from source under BSD/Linux 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).

获取源文件

首先,你需要一个已经安装的SVN客户端。使用你的软件包管理器,安装一个工具,像在Windows上的TortoiseSVN ,更多信息查看网页

使用命令行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

为更新上面已下载(已签出的)的源文件:

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_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...

cd ~
svn checkout http://svn.freepascal.org/svn/fpc/branches/fixes_3_2 fpc_fixes_3_2

照例更新:

cd ~
svn update fpc_fixes_3_2

文档源文件

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.

更多信息,查看Free Pascal网站

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

Installing from the ports collection

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.

这必需以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 [2020/06/14] 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

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:

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

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.

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:

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 environment variable before /usr/bin/.

在BSD/Linux上从源文件安装

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构建过程:

  • 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' FPC=/path/to/startingcompiler-name-ppcx64
# $MAKE is make on Linux and gmake on BSD 
# /path/to/ can be omitted when ppc386 (32 bit) or ppcx64 (64 bit) is in the path
  • Install FPC. Again in work/fpc, run
$MAKE install FPC=compiler/ppcx64 PREFIX=$THEPREFIX
#replace the FPC=compiler/ppcx64 (or ppc386 for 32 bit) with the relevant compiler if not on Intel x86
#THEPREFIX= usually is /usr/local or just /usr, but eg on NetBSD it is /usr/pkg for ports)
  • 创建符号链接:
ln -s $THEPREFIX/lib/fpc/3.2.0/ppcx64 $THEPREFIX/bin/ppcx64
  • 安装源文件:
$MAKE install sourceinstall PREFIX=$THEPREFIX
  • Create a symlink for default FPC source path:
ln -sf $THEPREFIX/share/src/3.2.0/fpc /usr/share/fpcsrc
  • 设置fpc.cfg配置文件:
$THEPREFIX/lib/fpc/3.2.0/samplecfg $THEPREFIX/lib/fpc/3.2.0 $ETCDIR
  • 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
    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

安装FPC二进制文件

到目前为止,获取一个可以工作的Free Pascal安装的最简单的方法是,从SourceForge存储库下载当前的二进制发布版本 - 发布版本包含当前的Free Pascal编译器和Free Pascal 库。

在Windows上从源文件安装

从源文件安装 -- 查看接下来的章节来了解如何获取源文件 -- 这不是针对新手的,因为你也需要一个最开始的编译器。

FPC源文件

<<<< 查看上述针对Linux的[#FPC_sources|FPC源文件]]的章节,在其中描述了SVN的使用 >>>>

通过SVN获取Free Pascal源文件是最简单的方法;更多信息查看下一章节。你也可以下载一个整个软件包 -- 发布树的每日快照版本查看 https://www.freepascal.org/develop.html

通过SVN获取FPC源文件

为了执行上述步骤,你将需要安装一个SVN客户端,例如TortoiseSVN。The exact commands vary between SVN clients; the ones given below are for the command line Subversion client downloadable from https://sourceforge.net/projects/win32svn/.

First create a directory in which you'd like to put the sources. Any normal user can do this. 为FPC创建一个目录(例如,C:\Source), then do the following at the command prompt:

C:\Source> svn checkout https://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 update

查看: https://www.freepascal.org/down/i386/win32.html。下载发行软件包(fpc-3.2.0.i386-win32.exe)并运行它 - 它是一个自解压安装器,因此只需要遵循安装说明来安装它。安装器应该会按情况设置PATH环境变量。

接下来重新启动windows。

在你安装FPC二进制文件后,你可以从中构建FPC源文件。

提示:

  • Windows (7+) 要求使用一个提升权限状态的用户命令提示符。从开始菜单中右键单击"命令提示符",并选择"以管理员身份运行"。
  • YOUR-PREFIX完全取决于你安装FPC的位置。At the time of this writing, the binaries are instructed to use a default location of "C:\FPC" 并且它们被放置到"C:\FPC\3.2.0"中。在Linux下, the make install scripts were adjusted to create a new sub-foldezr 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).

操作指南:

  • 在命令提示符中导航到本地的FPC源文件。例如,输入"cd c:\Developer\FPC"
  • 为构建FPC,输入"make all"
  • 为重写已存在的FPC,输入"make install PREFIX=YOUR-PREFIX"
  • 为安装源文件,输入"make install sourceinstall PREFIX=YOUR-PREFIX"

使用SVN (Win32)从源文件编译/安装FPC

FPC 3.2.0版本 或 trunk版本

步骤#1: 创建目录和获取源文件

创建下面的目录:

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

或对于fpc trunk版本来说:

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

你将需要最新的发布版本编译器来构建一个新的编译器。使用FTP(下面的地址)来获取ppc386(编译器),并解压缩它到c:\freepascal\binutils\

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

在安装TortoiseSVN后,使用针对每个目录的URL来从SVN中下载源文件,查看:

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

或对于fpc trunk版本来说:

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

或对于fpc trunk版本来说:

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

步骤 #2: 创建一个BAT文件来编译FPC

在下载完所有的东西后,我们需要一个BAT文件来编译FPC源文件。 创建一个新的文件c:\freepascal\makefpc.bat,并复制/粘贴下面的脚本:

@echo on
set myroot=c:\freepascal
set myFPC=%myroot%\fpc\3.2.0 
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

或对于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

步骤 #3: Make和安装FPC

在命令提示符中(cmd.exe),导航到目录c:\freepascal and execute the script we just wrote:

cd /d c:\freepascal
makefpc.bat

完成!

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.

步骤1:下载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

步骤2: Xcode命令行工具

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

步骤3:下载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.

其它的macOS安装选项

fpcupdeluxe

你也可以考虑使用fpcupdeluxe来安装FPC。

fink & MacPorts

Alternatively, you can use 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.

测试FPC安装

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.

现在使用这个命令行编译这个简单的代码:

fpc helloworld.pas <enter>

It should very quickly make an executable called, you guessed it, "helloworld".使用这个命令来运行这个可执行文件:

./helloworld <enter>

如果这样工作,很好地完成了!

安装交叉编译器

一个交叉编译器允许你为一个平台创建一个不同于正在使用的编译平台的二进制文件(可执行文件)。例如,在macOS下工作,并为Win32,FreeBSD或Linux创建可执行文件。 关于如何安装交叉编译器的详细情况,查看交叉编译

请参阅