Sign in Join
Wiki copytemporary

Cross Compile to RasPi from Linux/zh CN: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11### 介绍
22
33这个页面是关于设置交叉/跨平台编译器的,从Linux到[[Lazarus on Raspberry Pi|Raspberry Pi]]。你应该能够在lazarus中或在仅使用的FPC中选择arm作为目标CPU,选择使用ppcrossarm编译器和生成v6 arm二进制文件。Raspberry Pi只是一个arm的具体示例,在其它类似的平台上,只有需要一些微小的修改。
44
55- 在这里我们将讨论制作 armhf 软件包,而不是 armel 软件包,这里是一个关于低版本的简单讨论。
66- 在 Ubuntu 21.04 和 Debian 11 Bullseye系统上测试。它应该类似于大多数的Linux平台。
77- 假设**你当前已经有了一个FPC**,可能已经安装Lazarus。你必需使用来自FPC/Lazarus SourceForge网站下载的软件包,来自你的发行版中FPC和Lazarus软件包很可能不会如期工作。
88- 这份安装过程的一大部分是以root用户完成的。以root用户工作可能会弄坏你的系统,请注意。
99
1010**这个页面正在开发中,所提供的方法正在测试中**
1111
1212### 获取必要的库
1313
1414首先,我们需要一些来自你的 Raspberry Pi 的适当的当前的库, 这是在我竭尽所能后所测试出的获取必要库的最佳方式。确保你将需要的这些库(用于交叉/跨平台编译)已经安装在你的 Pi 上。
1515
1616> **Note:** 在Raspberry Pi上选择并制作Qt5应用程序可能更有意义,现在,Raspberry Pi操作系统的基本安装不再包括GTK2库。作为依赖项的Qt5比GTK2小很多。因此,在下一步之前,需要在Pi上安装Qt5依赖项:libqt5pas1 和 libqt5pas-dev,它们将带来必要的依赖项。如果你正在使用Lazarus主干版本(又称 trunk),接下来,查看 <https://github.com/davidbannon/libqt5pas>
1717
1818在 Pi 上,我们将归档我们所需要的文件,这不需要 root 权限来执行。
1919
20-`tar czf usr_lib_arm-linux-gnueabihf.tgz /usr/lib/arm-linux-gnueabihf`\
21-`tar czf lib_arm-linux-gnueabihf.tgz /lib/arm-linux-gnueabihf`
20+```text
21+tar czf usr_lib_arm-linux-gnueabihf.tgz /usr/lib/arm-linux-gnueabihf
22+tar czf lib_arm-linux-gnueabihf.tgz /lib/arm-linux-gnueabihf
23+```
2224
2325接下来,我们需要 crt 文件,首先,找到它们在哪里,你的文件可能在一个目录中, 在这里不是显示的'Debian 10' (它是Debian 11 bullseye).
2426
25-`raspberrypi:~ $ find / -name "crtbegin.o" 2>null`\
26-`/usr/lib/gcc/arm-linux-gnueabihf/10/crtbegin.o`\
27-`raspberrypi:~ $ (cd /usr/lib/gcc/arm-linux-gnueabihf/10 && tar czf $HOME/crt.tgz crt*.o)`
27+```text
28+raspberrypi:~ $ find / -name "crtbegin.o" 2>null
29+/usr/lib/gcc/arm-linux-gnueabihf/10/crtbegin.o
30+raspberrypi:~ $ (cd /usr/lib/gcc/arm-linux-gnueabihf/10 && tar czf $HOME/crt.tgz crt*.o)
31+```
2832
2933注意:与从一个拥有所以库的系统安装相比,从一个“最基础”安装中获取这些库可能是一个好主意,
3034
3135将这三个文件 248M (或使用Qt5的330k)、7M 和 3K 分别带到linux中,并将它们放置的你的home目录中,接下来,我们将以root用户来出来。 它们将占用大约640M的磁盘空间,可能会包含你永远不会使用的库,但是这样做比你找到真正需要的库要更容易。
3236
3337### 编译 fpc.cfg
3438
3539**有证据证明,这一步骤是非必要的,目前只需要忽略它,它需要更多的研究** 仅有一次更改是必需的,必需告诉编译器我们把哪个特定的arm cpu作为目标
3640
37-`#ifdef cpuarm`\
38-`-CpARMV6`\
39-`-CfVFPV2`\
40-`-OoFASTMATH`\
41-`#endif`
41+```text
42+#ifdef cpuarm
43+-CpARMV6
44+-CfVFPV2
45+-OoFASTMATH
46+#endif
47+```
4248
4349如果你的 fpc.cfg 是在 /etc中,它可能是在那里,你需要以root用户来编辑它,这没有关系,我们将以root用户来完成大量的工作。因此,从现在起,你将以root用户运行,这意味着必需即为小心。这些命令是需要逐个复制和粘贴的。它们不是脚本!
4450
4551### sudo
4652
4753好的,在这里,我们必需变成root用户,这意味着要非常小心!大多数的linux系统都允许运行sudo,如果不允许的话,尝试 su -
4854
4955如果你没有root用户的密码(或者,不想使用它),那么,重新配置这一整个系统环境,你可以使用一个fpc编译器和fpc源文件来安装基础的编译器到用户的空间,接着从Lazarus源文件构建Lazarus。再强调一次,在用户空间。很好的工作方法,你将需要相应地调整各种路径环境。
5056
51-`sudo -i`
57+```text
58+sudo -i
59+```
5260
5361### 安装 Pi 库
5462
5563接下来,我们需要安装必要的来自Pi的这些tar压缩包的库(我们先前移动到这里的)。新库最终在 /usr/lib/arm-linux-gnueabihf - 因此,如果需要的话,很容易清理。
5664
57-`cd / `\
58-`tar  xzf  /home/"$SUDO_USER"/usr_lib_arm-linux-gnueabihf.tgz `\
59-`tar  xzf  /home/"$SUDO_USER"/lib_arm-linux-gnueabihf.tgz`\
60-`tar  xzf  /home/"$SUDO_USER"/crt.tgz  -C  /usr/lib/arm-linux-gnueabihf `
65+```text
66+cd /
67+tar xzf /home/"$SUDO_USER"/usr_lib_arm-linux-gnueabihf.tgz
68+tar xzf /home/"$SUDO_USER"/lib_arm-linux-gnueabihf.tgz
69+tar xzf /home/"$SUDO_USER"/crt.tgz -C /usr/lib/arm-linux-gnueabihf
70+```
6171
6272接下来,安装一下必要的东西,并为其建立符号链接,以便Lazarus能找到它们。
6373
64-`apt install binutils-arm-linux-gnueabihf`\
65-`ln -s /usr/bin/arm-linux-gnueabihf-ld /usr/bin/arm-linux-ld`\
66-`ln -s /usr/bin/arm-linux-gnueabihf-as /usr/bin/arm-linux-as`\
67-`ln -s /usr/bin/arm-linux-gnueabihf-objcopy /usr/bin/arm-linux-objcopy`\
68-`ln -s /usr/bin/arm-linux-gnueabihf-strip /usr/bin/arm-linux-strip`
74+```text
75+apt install binutils-arm-linux-gnueabihf
76+ln -s /usr/bin/arm-linux-gnueabihf-ld /usr/bin/arm-linux-ld
77+ln -s /usr/bin/arm-linux-gnueabihf-as /usr/bin/arm-linux-as
78+ln -s /usr/bin/arm-linux-gnueabihf-objcopy /usr/bin/arm-linux-objcopy
79+ln -s /usr/bin/arm-linux-gnueabihf-strip /usr/bin/arm-linux-strip
80+```
6981
7082### 构建交叉/跨平台编译器
7183
7284现在,我们构建交叉/跨平台编译器,一系列已编译的arm单元。
7385
7486``` bash
7587 export FPCVER="3.2.2"
7688 cd /usr/share/fpcsrc/"$FPCVER"/
7789
7890 make clean all FPMAKEOPT="-T 4" CPU_TARGET=arm OPT="-dFPC_ARMHF" CROSSOPT="-CpARMV6 -CaEABIHF" CROSSBINDIR=/usr/arm-linux-gnueabihf/bin
7991
8092 make crossinstall OS_TARGET=linux CPU_TARGET=arm OPT="-dFPC_ARMHF" CROSSBINDIR=/usr/arm-linux-gnueabihf/bin CROSSOPT="-CpARMV6 -CaEABIHF" INSTALL_PREFIX=/usr
8193
8294 ln -sf /usr/lib/fpc/"$FPCVER"/ppcrossarm /usr/bin/ppcrossarm
8395```
8496
8597好的,完成工作了。按下Ctrl+D组合键来退出以root用户方式的访问和测试!
8698
8799### 测试
88100
89101测试fpc的实际效果很容易,将hello world源文件放入一个名称为hello.pas的文件中,然后,尝试 -
90102
91-`db@U2104G-VirtualBox:~/Pascal/console$ ppcrossarm hello.pas`\
92-`Free Pascal Compiler version 3.2.2 [2021/07/19] for arm`\
93-`Copyright (c) 1993-2021 by Florian Klaempfl and others`\
94-`Target OS: Linux for ARMEL`\
95-`Compiling hello.pas`\
96-`Linking hello`\
97-`4 lines compiled, 0.1 sec`
103+```text
104+db@U2104G-VirtualBox:~/Pascal/console$ ppcrossarm hello.pas
105+Free Pascal Compiler version 3.2.2 [2021/07/19] for arm
106+Copyright (c) 1993-2021 by Florian Klaempfl and others
107+Target OS: Linux for ARMEL
108+Compiling hello.pas
109+Linking hello
110+4 lines compiled, 0.1 sec
111+```
98112
99-`db@U2104G-VirtualBox:~/Pascal/console$ file hello`\
100-`hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.0.0, stripped`
113+```text
114+db@U2104G-VirtualBox:~/Pascal/console$ file hello
115+hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.0.0, stripped
116+```
101117
102118你可以将这个二进制文件移动到 Pi 中,并在其中测试它是否真的工作。
103119
104120测试Lazarus实际效果更复杂一些,启动Lazarus,打开一个新的工程,可以在其中设置一个或两个控件,打开 工程-\>工程选项-\>编译器选项-\>配置和目标,并在 目标平台 中,将 目标CPU族 设置为'arm'。
105121
106122编译和查看,二进制文件是否看起来像这样 -
107123
108-`db@U2104G-VirtualBox:~/Pascal/ArmTest$ file project1`\
109-`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`
124+```text
125+db@U2104G-VirtualBox:~/Pascal/ArmTest$ file project1
126+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
127+```
110128
111129### 更进一步的信息
112130
113131这个页面是关于制作**armhf**软件包的,它适合于使用硬件浮点功能的arm芯片。当前的Raspberry Pi是一个示例。另一个示例是**armel**或软浮点功能的arm芯片,它也经常出现在很多系统中。它有可能是从上面的命令行中删除了OPT="-dFPC_ARMHF"中的一部分,使其适用于软浮点功能的工作。这尚未经过测试!
114132
115133二进制文件可以是armhf或armel指令集中ArmV6或ArmV7 (甚至是ArmV8,但是适用于不同规则)。通常,软浮点功能arm软件包可以在一个Raspberry Pi上运行,但是,它也可能会混淆你的软件包管理器。再强调一次,这是理论上存在的。
116134
117135我发现**readelf**命令对查看一个编译后生成的二进制文件是很有用的,尝试 -
118136
119-`readelf -a mybinary | grep Tag `\
120-`...`\
121-`readelf -h mybinary `
137+```text
138+readelf -a mybinary | grep Tag
139+...
140+readelf -h mybinary
141+```
122142
123143另外一个有用的提示,在这个空间玩时,在更改FPC配置后,使用Lazarus的 Tools-\>RefreshFPCSource 菜单项。(注:原英文表述不明)
124144
125145## 参考
126146
127147- [[Cross compiling|交叉编译/跨平台编译]]
128148- [[Installing Lazarus|安装Lazarus]]
129149- [[Installing the Free Pascal Compiler|安装Free Pascal Compiler]]
130150
131151# 贡献者和更改
132152
133153- 简体中文版本由 robsean 于 2023-03-17 创建(2023-03-24完成全部翻译)。
134154
135155---
136156*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](/wiki-contact).*
137157Category: Cross compilation
138158Category: Linux

Back to history