Sign in Join
Wiki copytemporary

Cross Compile to RasPi from Linux/zh CN

English · 中文(中国)

介绍

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

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

获取必要的库

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

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

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

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

text
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作为目标

text
#ifdef cpuarm
-CpARMV6
-CfVFPV2
-OoFASTMATH
#endif

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

sudo

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

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

text
sudo -i

安装 Pi 库

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

text
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能找到它们。

text
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单元。

bash
 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的文件中,然后,尝试 -

text
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
text
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'。

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

text
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命令对查看一个编译后生成的二进制文件是很有用的,尝试 -

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

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

参考

贡献者和更改


From the Free Pascal wiki, page "Cross Compile to RasPi from Linux/zh CN", as it stood on 2023-03-24 after a change by Robsean, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2023

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
RobseanCreated page
RobseanInstall the Pi Libraries
RobseanGetting the necessary libraries
RobseanBuild the Cross Compiler
RobseanIntroduction
RobseanEdit fpc.cfg
Robsean编译 fpc.cfg
Robsean获取必要的库
Robsean获取必要的库
Robsean构建交叉/跨平台编译器
RobseanTest
RobseanFurther Information
Robseansudo
Robsean贡献者和更改

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.