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

From Lazarus wiki
Jump to navigationJump to search
Line 85: Line 85:
 
好的,完成工作了。按下Ctrl+D组合键来退出以root用户方式的访问和测试!
 
好的,完成工作了。按下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 100: 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

Revision as of 03:52, 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

OK, here we must become root, that implies very careful ! Most linux systems allow sudo, if not, try 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.

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

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 !

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.

I found the readelf command useful for looking at a resulting binary after compilation, try the following -

readelf -a mybinary | grep Tag [enter]
...
readelf -h mybinary [enter]

Another useful tip when playing in this space is to use Lazarus's Tools->RefreshFPCSource menu entry after making changes to the FPC config.

参考

贡献者和更改

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