Sign in Join
Wiki copytemporary

Setup Cross Compile For ARM: what changed

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

Title: Setup Cross Compile For ARM

OldNewLine
1+## How To setup Lazarus/FPC for arm-linux cross compiling.
2+
3+With this tutorial you can make a binary file that runs on arm-linux devices. This is tested with a Nokia N900 with "[Maemo 5 OS](http://maemo.org/)" (the gnu/linux Debian based OS, developed by [Nokia](http://maemo.nokia.com/videos/introducing-maemo-5/). It is an arm-linux device that runs GTK2 and QT4.6).
4+
5+If you need a simple "command prompt"/"terminal" program, after #FPC for ARM section skip to #APPENDIX A: Testing console programs.
6+
7+**NOTE 1**: All procedures are tested on a Virtual Machine with **Ubuntu 9.10 - x86** with Windows 7 as host and VirtualBox 3.0.12 as virtual machine. It is a fresh install, with all updates up to date.
8+
9+**NOTE 2**: Replace every */home/user/* with your real user path. All other paths are simply for explanation. If you use "[Maemo SDK Virtual Image](http://maemovmware.garage.maemo.org/2nd_edition/)", replace every */home/user/* with */home/maemo/*
10+
11+## Automated Script
12+
13+The previously provided scripts are obsolete. Please see [HERE](http://wiki.lazarus.freepascal.org/Lazarus_Manager) (see discussion page for alternative.
14+
15+## Generic things to do
16+
17+> **Warning:** These instructions are quite old - they refer to 2.2.4 while the current (December 2013) stable FPC is 2.6.2. Please adjust as you follow these and update the page. Thanks.
18+
19+### Downloads
20+
21+- any starting compiler from the 2.2.4 branch (e.g. fpc-2.2.4-20091214.i386.rpm) from [HERE](http://www.hu.freepascal.org/lazarus) or [HERE](http://snapshots.lazarus.shikami.org/)). It is needed to compile \>= 2.2.4 fpc sources
22+- latest binutils.tar.gz (e.g. binutils-2.20.tar.gz) from [HERE](http://ftp.gnu.org/gnu/binutils/)
23+- latest [fpc.zip](http://ftp//ftp.freepascal.org/pub/fpc/snapshot/trunk/source/fpc.zip) from [HERE](http://www.freepascal.org/develop.var)
24+- latest Lazarus source zip (e.g. lazarus-0.9.29-23129-20091214-src.zip) from [HERE](http://www.hu.freepascal.org/lazarus) or [HERE](http://snapshots.lazarus.shikami.org/) (faster for me)
25+- target libs (You can grab from SDK or from arm-device. *See "#APPENDIX A: Testing console programs"*)
26+
27+### Unpack/copy all files in the respective directories
28+
29+FPC 2.2.4 installer:
30+
31+`~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm`
32+
33+Binutils sources:
34+
35+`~/fpc_tools/binutils-2.20/`
36+
37+Lazarus sources:
38+
39+`~/lazarus/`
40+
41+FPC 2.5.1 sources:
42+
43+`~/lazarus/fpc/2.5.1/`
44+
45+### Prepare Target Libs
46+
47+- See "#APPENDIX A: Testing console programs" to know how/where to find target libs
48+- Copy all target libs to ~/lazarus/fpc/libcross
49+
50+### Install dependencies
51+
52+``` bash
53+sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev
54+```
55+
56+## Install & Configure Binutils
57+
58+### Compile binutils for arm-linux
59+
60+- If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
61+- Then type:
62+
63+``` bash
64+cd ~/fpc_tools/binutils-2.20
65+./configure --target=arm-linux --disable-werror
66+make
67+sudo make install
68+mkdir ~/lazarus/fpc/binutils
69+ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
70+ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
71+sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
72+
73+sudo gedit /usr/local/bin/arm-linux-as
74+```
75+
76+- Paste:
77+
78+``` bash
79+#!/bin/sh
80+/usr/local/bin/arm-linux-as_org -meabi=5 $@
81+```
82+
83+**NOTE**: option *-meabi=5* force assembler to make binary *eabi v5* compatible. E.g. if you need eabi v4 you will change it in *-meabi=4*.
84+
85+- Save and close gedit
86+- Type:
87+
88+``` bash
89+sudo chmod +x /usr/local/bin/arm-linux-as
90+ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as
91+```
92+
93+## FPC
94+
95+### Install precompiled fpc 2.2.4 branch
96+
97+**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
98+
99+- Type:
100+
101+``` bash
102+sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
103+```
104+
105+- Be sure that ppc386 is right installed, type:
106+
107+``` bash
108+which ppc386
109+```
110+
111+if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)
112+
113+### Compile FPC from sources
114+
115+- Type:
116+
117+``` bash
118+cd ~/lazarus/fpc/2.5.1
119+make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
120+sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
121+sudo rm /usr/bin/ppc386
122+sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
123+sudo mkdir /usr/share/fpcsrc/
124+sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
125+sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc
126+```
127+
128+## FPC for ARM
129+
130+### Make FPC able to cross compile for arm-linux
131+
132+- Type:
133+
134+``` bash
135+cd ~/lazarus/fpc/2.5.1/
136+sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr
137+```
138+
139+### Create custom fpc.cfg
140+
141+**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
142+
143+- Type:
144+
145+``` bash
146+gedit ~/.fpc.cfg
147+```
148+
149+- Paste in ~/.fpc.cfg:
150+
151+ #INCLUDE /etc/fpc.cfg
152+ #IFDEF CPUARM
153+ -Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
154+ -Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
155+ -XP/home/user/lazarus/fpc/binutils/
156+ -Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
157+ -Xr/home/user/lazarus/fpc/libcross
158+ -XR/home/user/lazarus/fpc/
159+ -darm
160+ -Tlinux
161+ #ENDIF
162+
163+- save and close gedit
164+- Type:
165+
166+``` bash
167+sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm
168+```
169+
170+## Lazarus
171+
172+If you don't need graphical programs, you can skip this section and go directly to #Appendix B: Target libs
173+
174+### Compile Lazarus
175+
176+- Type:
177+
178+``` bash
179+cd ~/lazarus
180+make clean all
181+```
182+
183+### Start Lazarus
184+
185+- Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
186+- Type:
187+
188+``` bash
189+./lazarus
190+```
191+
192+### Configure Lazarus for cross Compile
193+
194+- Now you must cross compile the LCL and Package Registration, this part comes from [HERE](http://wiki.lazarus.freepascal.org/Cross_compiling#Cross_compiling_the_LCL).
195+- From the IDE:
196+ 1. In Tools -\> Options -\> Environment -\> Files, set the Compiler path to the path to fpc. Normally this is already done.
197+ 2. Then open Tools / Configure Build Lazarus / Advanced Build Options.
198+ 3. Set Target OS as *linux* and Target CPU as *arm*.
199+ 4. Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
200+ 5. Click the Build button.
201+
202+## Make your first arm-linux project in Lazarus
203+
204+### New Project
205+
206+- Start new project as application
207+- Put some component in main form to test (like button, memo and few other things)
208+
209+### Set Targets
210+
211+- From the IDE:
212+ 1. Set in Project / Compiler Options / Code generation / Target OS (-T): *Linux*
213+ 2. Set in Project / Compiler Options / Code generation / Target CPU family (-P): *arm*
214+ 3. Set in Project / Compiler Options / Other / Custom Option: enter -XParm-linux- into textbox
215+
216+### Add target libs to your project
217+
218+**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
219+
220+- From the IDE:
221+ 1. Set in Project / Compiler Options / Linking / Options (-k): *-L/home/user/lazarus/fpc/libcross*
222+ 2. Set in Project / Compiler Options / Compilation / Execute after / Command: *arm-linux-strip -s /path/to/your/project/binary* (where *"binary"* is the executable builded with Lazarus)
223+
224+### Build Project
225+
226+- Build Project as normal:
227+ 1. Hit keys Ctrl+F9
228+ 2. Run / Build
229+
230+### Time to run
231+
232+- Transfer the binary of your project on device
233+- Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
234+
235+``` bash
236+chmod +x /path/where/is/YourProject
237+./path/where/is/YourProject
238+```
239+
240+<figure>
241+<img src="Kjow_hello-world.png" title="e.g. Maemo 5 running Lazarus graphical demo." width="300" />
242+<figcaption>e.g. Maemo 5 running Lazarus graphical demo.</figcaption>
243+</figure>
244+
245+## APPENDIX A: Testing console programs
246+
247+### Create a TestARM.pas
248+
249+- Type:
250+
251+``` bash
252+mkdir ~/dev/TestARM/
253+gedit ~/dev/TestARM/TestARM.pas
254+```
255+
256+- Paste this code:
257+
258+``` pascal
259+program test;
260+begin
261+ writeln('DATE ',{$i %DATE%});
262+ writeln('FPCTARGET ',{$i %FPCTARGET%});
263+ writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
264+ writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
265+ writeln('FPCVERSION ',{$i %FPCVERSION%});
266+end.
267+```
268+
269+- Type:
270+
271+``` bash
272+cd ~/dev/TestARM/
273+fpc -Tlinux -Parm -XParm-linux- TestARM.pas
274+```
275+
276+### Time to run
277+
278+- Transfer TestARM on device
279+- Generally you must give executable permission to *TestARM* directly on the device:
280+
281+``` bash
282+chmod +x /path/where/is/TestAMR
283+./path/where/is/TestAMR
284+```
285+
286+## Appendix B: Target libs
287+
288+You need target libs to assemble and link binary file. You can find them into the SDK of your device or into the device itself.
289+
290+E.g. for Nokia N900 you need to copy /lib/* and /usr/lib/* (no subdirs needed) that are present in the Nokia N900 root and you must merge all files to ~/lazarus/fpc/libcross/\
291+Finally, you need to rename each library *not found* by linker (ld) to the file name searched by linker itself.\
292+E.g. if you got this error
293+
294+`libX11.so not found (or incompatible)`
295+
296+and you have a file called *libX11.so.6.2.0*, then you need to rename it into *libX11.so*
297+
298+## See also
299+
300+- [[Android]] Article on setting up cross-compiling on Windows for Android on ARM
301+- [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
302+
303+---
304+*From the Free Pascal wiki, page "Setup Cross Compile For ARM", as it stood on 2020-11-26 after a change by Trev, 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).*

Back to history