Sign in Join
Wiki copytemporary

Cross Compile to RasPi from Linux: what changed

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

OldNewLine
11### Introduction
22
33This 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.
44
55- Here we talk about making armhf software, not armel, there is a very brief discussion about that lower down.
66- Tested on an Ubuntu 21.04 and Debian Bullseye System. Its should be similar on most mainstream Linux platforms.
77- 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.
88- 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.
99
1010**This page is under development and the recipe presented is still being tested.**
1111
1212### Getting the necessary libraries
1313
1414Firstly, we are going to need some reasonably current libraries from your Raspberry Pi, as near as I can determine this is the best way to get the necessary libraries. Make sure the libraries you will need (to cross compile) are already installed on your pi.
1515
1616> **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 <https://github.com/davidbannon/libqt5pas>
1717
1818On the pi, we will tar up what we need, this does not need to be done as 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
2325Next, we need the crt files, first, find where they are, yours may be in a directory other than the '10' shown here (thats 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
2933Note: is probably a good idea to take these libraries from a "minimal" install rather than one with all the bells and whistles. But do make sure the libraries (and the dev version) that you will need are installed. Messy otherwise.
3034
3135Take these three files, 248M (or 330M 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. 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.
3236
3337### Edit fpc.cfg
3438
3539**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.
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
4349If 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 !
4450
4551### sudo
4652
4753OK, here we must become root, that implies very careful ! Most linux systems allow sudo, if not, try su -
4854
4955If 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.
5056
51-`sudo -i`
57+```text
58+sudo -i
59+```
5260
5361### Install the Pi Libraries
5462
5563Next, we need to install the needed libraries from a Pi from those tar balls we moved over before. The new libraries all end up in /usr/lib/arm-linux-gnueabihf - so easy clean up if necessary.
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
6272Next, install some needed things and symlink them up to what Lazarus expects to find.
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### Build the Cross Compiler
7183
7284OK, now we build the cross compiler and a set of pre compiled arm units.
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
8597OK, done, Control-D to get out of that somewhat dangerous root access and test !
8698
8799### Test
88100
89101Testing fpc functionality is easy, put the hello world source in a file, hello.pas and try this -
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
102118You can move that binary over to a Pi and test it really works. Note this test uses no additional libraries so is not a complete test.
103119
104120Testing 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'. You probably will find a few missing dev libraries, remember that are usually just symlinks from the actual library to the base name, easier to make that link than go back to your Pi.
105121
106122Compile and see if the binary looks like this -
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### Further Information
112130
113131This 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 !
114132
115133Binaries 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.
116134
117135I found the **readelf** command useful for looking at a resulting binary after compilation, try the following -
118136
119-`readelf -a mybinary | grep Tag [enter]`\
120-`...`\
121-`readelf -h mybinary [enter]`
137+```text
138+readelf -a mybinary | grep Tag [enter]
139+...
140+readelf -h mybinary [enter]
141+```
122142
123143Another useful tip when playing in this space is to use Lazarus's Tools-\>RefreshFPCSource menu entry after making changes to the FPC config.
124144
125145## See Also
126146
127147- [[Cross compiling|Cross Compiling]]
128148- [[Installing Lazarus]]
129149- [[Installing the Free Pascal Compiler]]
130150
131151---
132152*From the Free Pascal wiki, page "Cross Compile to RasPi from Linux", as it stood on 2023-10-10 after a change by Dbannon, 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).*
133153Category: Cross compilation
134154Category: Linux

Back to history