Difference between revisions of "Cross Compile to RasPi from Linux/zh CN"

From Lazarus wiki
Jump to navigationJump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
  
  
=== Introduction ===
+
=== 介绍 ===
  
This page is about setting up a Cross Compiler from a Linux Box to a [[Lazarus_on_Raspberry_Pi | Raspberry Pi]]. You should be able to select arm as the CPU in Lazarus or if just using FPC, choose to use the ppcrossarm compiler and produce v6 arm binaries. Raspberry Pi is just a specific example of arm, only small changes would seem necessary on other similar platforms.
+
这个页面是关于设置交叉/跨平台编译器的,从Linux到[[Lazarus_on_Raspberry_Pi | Raspberry Pi]]。你应该能够在lazarus中或在仅使用的FPC中选择arm作为目标CPU,选择使用ppcrossarm编译器和生成v6 arm二进制文件。Raspberry Pi只是一个arm的具体示例,在其它类似的平台上,只有需要一些微小的修改。
  
* Here we talk about making armhf software, not armel, there is a very brief discussion about that lower down.
+
* 在这里我们将讨论制作 armhf 软件包,而不是 armel 软件包,这里是一个关于低版本的简单讨论。
* Tested on an Ubuntu 21.04 and Debian Bullseye System. Its should be similar on most mainstream Linux platforms.
+
* Ubuntu 21.04 Debian 11 Bullseye系统上测试。它应该类似于大多数的Linux平台。
* This recipe assumes '''you already have a current FPC''' and, possibly Lazarus installed. You MUST use packages downloaded from the FPC/Lazarus SourceForge site, its most likely that FPC and Lazarus packages from your Distribution will not work as expected.
+
* 假设'''你当前已经有了一个FPC''',可能已经安装Lazarus。你必需使用来自FPC/Lazarus SourceForge网站下载的软件包,来自你的发行版中FPC和Lazarus软件包很可能不会如期工作。
* A large part of this install is done as root. Working like that gives you the potential to make a real mess of your system, please be very careful.
+
* 这份安装过程的一大部分是以root用户完成的。以root用户工作可能会弄坏你的系统,请注意。
  
'''This page is under development and the recipe presented is still being tested.'''
+
'''这个页面正在开发中,所提供的方法正在测试中'''
  
 
=== 获取必要的库 ===
 
=== 获取必要的库 ===
  
首先,我们需要一些来自你的 Raspberry Pi 的适当的当前的库, as near as I can determine this is the best way to get the necessary libraries.确保你将需要的这些库(用于交叉/跨平台编译)已经安装在你的 Pi 上。  
+
首先,我们需要一些来自你的 Raspberry Pi 的适当的当前的库, 这是在我竭尽所能后所测试出的获取必要库的最佳方式。确保你将需要的这些库(用于交叉/跨平台编译)已经安装在你的 Pi 上。  
{{Note|It possibly makes good sense to choose to make Qt5 apps for the Raspberry Pi now that the basic install of the Raspberry Pi Operating System does not include GTK2 libraries anymore. Qt5 as a dependency is a lot smaller than GTK2. So, install the necessary Qt5 libraries on the Pi before the next step, libqt5pas1 and libqt5pas-dev, they will bring along the necessary dependencies hopefully. If you are using Lazarus Main (aka trunk) then look at
+
{{Note| 在Raspberry Pi上选择并制作Qt5应用程序可能更有意义,现在,Raspberry Pi操作系统的基本安装不再包括GTK2库。作为依赖项的Qt5比GTK2小很多。因此,在下一步之前,需要在Pi上安装Qt5依赖项:libqt5pas1 和 libqt5pas-dev,它们将带来必要的依赖项。如果你正在使用Lazarus主干版本(又称 trunk),接下来,查看 https://github.com/davidbannon/libqt5pas }}
https://github.com/davidbannon/libqt5pas }}
 
  
在 Pi 上,我们将归档我们所需要的大小,这不需要 root 权限来执行。
+
在 Pi 上,我们将归档我们所需要的文件,这不需要 root 权限来执行。
  
 
  tar czf usr_lib_arm-linux-gnueabihf.tgz /usr/lib/arm-linux-gnueabihf
 
  tar czf usr_lib_arm-linux-gnueabihf.tgz /usr/lib/arm-linux-gnueabihf
 
  tar czf lib_arm-linux-gnueabihf.tgz /lib/arm-linux-gnueabihf
 
  tar czf lib_arm-linux-gnueabihf.tgz /lib/arm-linux-gnueabihf
  
接下来,我们需要 crt 文件,首先,找到它们在哪里,你的文件可能在一个目录中, other than the '10' shown here (thats bullseye).
+
接下来,我们需要 crt 文件,首先,找到它们在哪里,你的文件可能在一个目录中, 在这里不是显示的'Debian 10' (它是Debian 11 bullseye).
  
 
  raspberrypi:~ $ find / -name "crtbegin.o" 2>null
 
  raspberrypi:~ $ find / -name "crtbegin.o" 2>null
Line 29: Line 28:
 
  raspberrypi:~ $ (cd /usr/lib/gcc/arm-linux-gnueabihf/10 && tar czf $HOME/crt.tgz crt*.o)
 
  raspberrypi:~ $ (cd /usr/lib/gcc/arm-linux-gnueabihf/10 && tar czf $HOME/crt.tgz crt*.o)
  
Note: is probably a good idea to take these libraries from a "minimal" install rather than one with all the bells and whistles.
+
注意:与从一个拥有所以库的系统安装相比,从一个“最基础”安装中获取这些库可能是一个好主意,
  
Take these three files, 248M (or 330k with Qt5), 7M and 3K respectively to the Intel box and put them in your home directory, then, we will deal with them as root further down.
+
将这三个文件 248M (或使用Qt5的330k)、7M 和 3K 分别带到linux中,并将它们放置的你的home目录中,接下来,我们将以root用户来出来。
They will use up some 640M of disk and probably includes libraries you will never use but its easier than trying to work out exactly what you do need.
+
它们将占用大约640M的磁盘空间,可能会包含你永远不会使用的库,但是这样做比你找到真正需要的库要更容易。
  
=== Edit fpc.cfg ===
+
=== 编译 fpc.cfg ===
'''Evidence is this step is unnecessary, just ignore it for now, further research is indicated'''
+
'''有证据证明,这一步骤是非必要的,目前只需要忽略它,它需要更多的研究'''
Only one change is necessary, must tell the compiler which particular arm cpu we are targeting.
+
仅有一次更改是必需的,必需告诉编译器我们把哪个特定的arm cpu作为目标
  
 
  #ifdef cpuarm
 
  #ifdef cpuarm
Line 44: Line 43:
 
  #endif
 
  #endif
  
If your fpc.cfg is in /etc and it probably is, you will need to do that edit as root, thats OK, we are about to do a whole load of stuff as root. So, from now on, you will be running as root, that means great care must be used. Note these instructions are meant to be copied and pasted, one by one. They are not a script !
+
如果你的 fpc.cfg 是在 /etc中,它可能是在那里,你需要以root用户来编辑它,这没有关系,我们将以root用户来完成大量的工作。因此,从现在起,你将以root用户运行,这意味着必需即为小心。这些命令是需要逐个复制和粘贴的。它们不是脚本!
  
 
=== sudo ===
 
=== sudo ===
  
OK, here we must become root, that implies very careful ! Most linux systems allow sudo, if not, try su -
+
好的,在这里,我们必需变成root用户,这意味着要非常小心!大多数的linux系统都允许运行sudo,如果不允许的话,尝试 su -
  
If you do not have the root password (or don't want to use it), then reconside this whole operation, you can install the base compiler into user space from a tarball and build Lazarus from source, again in user space. Pretty good way to work, you will need to adjust the various paths accordingly.  
+
如果你没有root用户的密码(或者,不想使用它),那么,重新配置这一整个系统环境,你可以使用一个fpc编译器和fpc源文件来安装基础的编译器到用户的空间,接着从Lazarus源文件构建Lazarus。再强调一次,在用户空间。很好的工作方法,你将需要相应地调整各种路径环境。  
  
 
  sudo -i
 
  sudo -i
Line 84: Line 83:
 
  ln -sf /usr/lib/fpc/"$FPCVER"/ppcrossarm /usr/bin/ppcrossarm</syntaxhighlight>
 
  ln -sf /usr/lib/fpc/"$FPCVER"/ppcrossarm /usr/bin/ppcrossarm</syntaxhighlight>
  
OK, done, Control-D to get out of that somewhat dangerous root access and test !
+
好的,完成工作了。按下Ctrl+D组合键来退出以root用户方式的访问和测试!
  
=== Test ===
+
=== 测试 ===
  
Testing fpc functionality is easy, put the hello world source in a file, hello.pas and try this -
+
测试fpc的实际效果很容易,将hello world源文件放入一个名称为hello.pas的文件中,然后,尝试 -
  
 
  db@U2104G-VirtualBox:~/Pascal/console$ ppcrossarm hello.pas
 
  db@U2104G-VirtualBox:~/Pascal/console$ ppcrossarm hello.pas
Line 101: Line 100:
 
  hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.0.0, stripped
 
  hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.0.0, stripped
  
You can move that binary over to a Pi and test it really works.
+
你可以将这个二进制文件移动到 Pi 中,并在其中测试它是否真的工作。
  
Testing the Lazarus functionality is more involved, fire up Lazarus, open a new project, maybe set a control or two on it, open Project->ProjectOptions->ConfigAndTarget and set ProjectCPUFamily to 'arm'.
+
测试Lazarus实际效果更复杂一些,启动Lazarus,打开一个新的工程,可以在其中设置一个或两个控件,打开 工程->工程选项->编译器选项->配置和目标,并在 目标平台 中,将 目标CPU族 设置为'arm'
  
Compile and see if the binary looks like this -
+
编译和查看,二进制文件是否看起来像这样 -
  
 
  db@U2104G-VirtualBox:~/Pascal/ArmTest$ file project1
 
  db@U2104G-VirtualBox:~/Pascal/ArmTest$ file project1
 
  project1: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 2.0.0, not stripped
 
  project1: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 2.0.0, not stripped
  
===Further Information===
+
===更进一步的信息===
This page is about making '''armhf''' software, that suits arm chips using hardware floating point capabilities. The current Raspberry Pi is an example. The alternative is the '''armel''' or soft float arm chip, it too appears in many systems. It is possible that omitting the OPT="-dFPC_ARMHF" bit from the above command lines may make it work for soft float. That has not been tested !
+
这个页面是关于制作'''armhf'''软件包的,它适合于使用硬件浮点功能的arm芯片。当前的Raspberry Pi是一个示例。另一个示例是'''armel'''或软浮点功能的arm芯片,它也经常出现在很多系统中。它有可能是从上面的命令行中删除了OPT="-dFPC_ARMHF"中的一部分,使其适用于软浮点功能的工作。这尚未经过测试!
  
Binaries can be ArmV6 or ArmV7 (or even ArmV8 but different rules apply) in either armhf or armel instruction set. Generally soft float arm software may run on (eg) a Raspberry Pi but it may also confuse your package manager. Again, theoretical advice.
+
二进制文件可以是armhf或armel指令集中ArmV6或ArmV7 (甚至是ArmV8,但是适用于不同规则)。通常,软浮点功能arm软件包可以在一个Raspberry Pi上运行,但是,它也可能会混淆你的软件包管理器。再强调一次,这是理论上存在的。
  
I found the '''readelf''' command useful for looking at a resulting binary after compilation, try the following -
+
我发现'''readelf'''命令对查看一个编译后生成的二进制文件是很有用的,尝试 -
  
  readelf -a mybinary | grep Tag [enter]
+
  readelf -a mybinary | grep Tag  
 
  ...
 
  ...
  readelf -h mybinary [enter]
+
  readelf -h mybinary  
  
Another useful tip when playing in this space is to use Lazarus's Tools->RefreshFPCSource menu entry after making changes to the FPC config.
+
另外一个有用的提示,在这个空间玩时,在更改FPC配置后,使用Lazarus的 Tools->RefreshFPCSource 菜单项。(注:原英文表述不明)
  
 
== 参考 ==
 
== 参考 ==
Line 132: Line 131:
  
 
= 贡献者和更改 =
 
= 贡献者和更改 =
* 简体中文版本由 [[User:Robsean | robsean]] 于 2023-03-17 创建(2023-03-20完成全部翻译)。
+
* 简体中文版本由 [[User:Robsean | robsean]] 于 2023-03-17 创建(2023-03-24完成全部翻译)。

Latest revision as of 04:23, 24 March 2023


介绍

这个页面是关于设置交叉/跨平台编译器的,从Linux到 Raspberry Pi。你应该能够在lazarus中或在仅使用的FPC中选择arm作为目标CPU,选择使用ppcrossarm编译器和生成v6 arm二进制文件。Raspberry Pi只是一个arm的具体示例,在其它类似的平台上,只有需要一些微小的修改。

  • 在这里我们将讨论制作 armhf 软件包,而不是 armel 软件包,这里是一个关于低版本的简单讨论。
  • 在 Ubuntu 21.04 和 Debian 11 Bullseye系统上测试。它应该类似于大多数的Linux平台。
  • 假设你当前已经有了一个FPC,可能已经安装Lazarus。你必需使用来自FPC/Lazarus SourceForge网站下载的软件包,来自你的发行版中FPC和Lazarus软件包很可能不会如期工作。
  • 这份安装过程的一大部分是以root用户完成的。以root用户工作可能会弄坏你的系统,请注意。

这个页面正在开发中,所提供的方法正在测试中

获取必要的库

首先,我们需要一些来自你的 Raspberry Pi 的适当的当前的库, 这是在我竭尽所能后所测试出的获取必要库的最佳方式。确保你将需要的这些库(用于交叉/跨平台编译)已经安装在你的 Pi 上。

Light bulb  Note: 在Raspberry Pi上选择并制作Qt5应用程序可能更有意义,现在,Raspberry Pi操作系统的基本安装不再包括GTK2库。作为依赖项的Qt5比GTK2小很多。因此,在下一步之前,需要在Pi上安装Qt5依赖项:libqt5pas1 和 libqt5pas-dev,它们将带来必要的依赖项。如果你正在使用Lazarus主干版本(又称 trunk),接下来,查看 https://github.com/davidbannon/libqt5pas

在 Pi 上,我们将归档我们所需要的文件,这不需要 root 权限来执行。

tar czf usr_lib_arm-linux-gnueabihf.tgz /usr/lib/arm-linux-gnueabihf
tar czf lib_arm-linux-gnueabihf.tgz /lib/arm-linux-gnueabihf

接下来,我们需要 crt 文件,首先,找到它们在哪里,你的文件可能在一个目录中, 在这里不是显示的'Debian 10' (它是Debian 11 bullseye).

raspberrypi:~ $ find / -name "crtbegin.o" 2>null
/usr/lib/gcc/arm-linux-gnueabihf/10/crtbegin.o
raspberrypi:~ $ (cd /usr/lib/gcc/arm-linux-gnueabihf/10 && tar czf $HOME/crt.tgz crt*.o)

注意:与从一个拥有所以库的系统安装相比,从一个“最基础”安装中获取这些库可能是一个好主意,

将这三个文件 248M (或使用Qt5的330k)、7M 和 3K 分别带到linux中,并将它们放置的你的home目录中,接下来,我们将以root用户来出来。 它们将占用大约640M的磁盘空间,可能会包含你永远不会使用的库,但是这样做比你找到真正需要的库要更容易。

编译 fpc.cfg

有证据证明,这一步骤是非必要的,目前只需要忽略它,它需要更多的研究 仅有一次更改是必需的,必需告诉编译器我们把哪个特定的arm cpu作为目标

#ifdef cpuarm
-CpARMV6
-CfVFPV2
-OoFASTMATH
#endif

如果你的 fpc.cfg 是在 /etc中,它可能是在那里,你需要以root用户来编辑它,这没有关系,我们将以root用户来完成大量的工作。因此,从现在起,你将以root用户运行,这意味着必需即为小心。这些命令是需要逐个复制和粘贴的。它们不是脚本!

sudo

好的,在这里,我们必需变成root用户,这意味着要非常小心!大多数的linux系统都允许运行sudo,如果不允许的话,尝试 su -

如果你没有root用户的密码(或者,不想使用它),那么,重新配置这一整个系统环境,你可以使用一个fpc编译器和fpc源文件来安装基础的编译器到用户的空间,接着从Lazarus源文件构建Lazarus。再强调一次,在用户空间。很好的工作方法,你将需要相应地调整各种路径环境。

sudo -i

安装 Pi 库

接下来,我们需要安装必要的来自Pi的这些tar压缩包的库(我们先前移动到这里的)。新库最终在 /usr/lib/arm-linux-gnueabihf - 因此,如果需要的话,很容易清理。

cd / 
tar  xzf  /home/"$SUDO_USER"/usr_lib_arm-linux-gnueabihf.tgz 
tar  xzf  /home/"$SUDO_USER"/lib_arm-linux-gnueabihf.tgz
tar  xzf  /home/"$SUDO_USER"/crt.tgz  -C  /usr/lib/arm-linux-gnueabihf 

接下来,安装一下必要的东西,并为其建立符号链接,以便Lazarus能找到它们。

apt install binutils-arm-linux-gnueabihf
ln -s /usr/bin/arm-linux-gnueabihf-ld /usr/bin/arm-linux-ld
ln -s /usr/bin/arm-linux-gnueabihf-as /usr/bin/arm-linux-as
ln -s /usr/bin/arm-linux-gnueabihf-objcopy /usr/bin/arm-linux-objcopy
ln -s /usr/bin/arm-linux-gnueabihf-strip /usr/bin/arm-linux-strip

构建交叉/跨平台编译器

现在,我们构建交叉/跨平台编译器,一系列已编译的arm单元。

 export FPCVER="3.2.2"
 cd /usr/share/fpcsrc/"$FPCVER"/

 make clean all FPMAKEOPT="-T 4" CPU_TARGET=arm OPT="-dFPC_ARMHF" CROSSOPT="-CpARMV6 -CaEABIHF" CROSSBINDIR=/usr/arm-linux-gnueabihf/bin

 make crossinstall OS_TARGET=linux CPU_TARGET=arm OPT="-dFPC_ARMHF" CROSSBINDIR=/usr/arm-linux-gnueabihf/bin   CROSSOPT="-CpARMV6 -CaEABIHF" INSTALL_PREFIX=/usr

 ln -sf /usr/lib/fpc/"$FPCVER"/ppcrossarm /usr/bin/ppcrossarm

好的,完成工作了。按下Ctrl+D组合键来退出以root用户方式的访问和测试!

测试

测试fpc的实际效果很容易,将hello world源文件放入一个名称为hello.pas的文件中,然后,尝试 -

db@U2104G-VirtualBox:~/Pascal/console$ ppcrossarm hello.pas
Free Pascal Compiler version 3.2.2 [2021/07/19] for arm
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for ARMEL
Compiling hello.pas
Linking hello
4 lines compiled, 0.1 sec
db@U2104G-VirtualBox:~/Pascal/console$ file hello
hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.0.0, stripped

你可以将这个二进制文件移动到 Pi 中,并在其中测试它是否真的工作。

测试Lazarus实际效果更复杂一些,启动Lazarus,打开一个新的工程,可以在其中设置一个或两个控件,打开 工程->工程选项->编译器选项->配置和目标,并在 目标平台 中,将 目标CPU族 设置为'arm'。

编译和查看,二进制文件是否看起来像这样 -

db@U2104G-VirtualBox:~/Pascal/ArmTest$ file project1
project1: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 2.0.0, not stripped

更进一步的信息

这个页面是关于制作armhf软件包的,它适合于使用硬件浮点功能的arm芯片。当前的Raspberry Pi是一个示例。另一个示例是armel或软浮点功能的arm芯片,它也经常出现在很多系统中。它有可能是从上面的命令行中删除了OPT="-dFPC_ARMHF"中的一部分,使其适用于软浮点功能的工作。这尚未经过测试!

二进制文件可以是armhf或armel指令集中ArmV6或ArmV7 (甚至是ArmV8,但是适用于不同规则)。通常,软浮点功能arm软件包可以在一个Raspberry Pi上运行,但是,它也可能会混淆你的软件包管理器。再强调一次,这是理论上存在的。

我发现readelf命令对查看一个编译后生成的二进制文件是很有用的,尝试 -

readelf -a mybinary | grep Tag 
...
readelf -h mybinary 

另外一个有用的提示,在这个空间玩时,在更改FPC配置后,使用Lazarus的 Tools->RefreshFPCSource 菜单项。(注:原英文表述不明)

参考

贡献者和更改

  • 简体中文版本由 robsean 于 2023-03-17 创建(2023-03-24完成全部翻译)。