Sign in Join
Wiki copytemporary

Setup Cross Compile For ARM: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## How To setup Lazarus/FPC for arm-linux cross compiling.
22
33With 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).
44
55If you need a simple "command prompt"/"terminal" program, after #FPC for ARM section skip to #APPENDIX A: Testing console programs.
66
77**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.
88
99**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/*
1010
1111## Automated Script
1212
1313The previously provided scripts are obsolete. Please see [HERE](http://wiki.lazarus.freepascal.org/Lazarus_Manager) (see discussion page for alternative.
1414
1515## Generic things to do
1616
1717> **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.
1818
1919### Downloads
2020
2121- 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
2222- latest binutils.tar.gz (e.g. binutils-2.20.tar.gz) from [HERE](http://ftp.gnu.org/gnu/binutils/)
2323- latest [fpc.zip](http://ftp//ftp.freepascal.org/pub/fpc/snapshot/trunk/source/fpc.zip) from [HERE](http://www.freepascal.org/develop.var)
2424- 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)
2525- target libs (You can grab from SDK or from arm-device. *See "#APPENDIX A: Testing console programs"*)
2626
2727### Unpack/copy all files in the respective directories
2828
2929FPC 2.2.4 installer:
3030
31-`~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm`
31+```text
32+~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
33+```
3234
3335Binutils sources:
3436
35-`~/fpc_tools/binutils-2.20/`
37+```text
38+~/fpc_tools/binutils-2.20/
39+```
3640
3741Lazarus sources:
3842
39-`~/lazarus/`
43+```text
44+~/lazarus/
45+```
4046
4147FPC 2.5.1 sources:
4248
43-`~/lazarus/fpc/2.5.1/`
49+```text
50+~/lazarus/fpc/2.5.1/
51+```
4452
4553### Prepare Target Libs
4654
4755- See "#APPENDIX A: Testing console programs" to know how/where to find target libs
4856- Copy all target libs to ~/lazarus/fpc/libcross
4957
5058### Install dependencies
5159
5260``` bash
5361sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev
5462```
5563
5664## Install & Configure Binutils
5765
5866### Compile binutils for arm-linux
5967
6068- If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
6169- Then type:
6270
6371``` bash
6472cd ~/fpc_tools/binutils-2.20
6573./configure --target=arm-linux --disable-werror
6674make
6775sudo make install
6876mkdir ~/lazarus/fpc/binutils
6977ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
7078ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
7179sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
7280
7381sudo gedit /usr/local/bin/arm-linux-as
7482```
7583
7684- Paste:
7785
7886``` bash
7987#!/bin/sh
8088/usr/local/bin/arm-linux-as_org -meabi=5 $@
8189```
8290
8391**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*.
8492
8593- Save and close gedit
8694- Type:
8795
8896``` bash
8997sudo chmod +x /usr/local/bin/arm-linux-as
9098ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as
9199```
92100
93101## FPC
94102
95103### Install precompiled fpc 2.2.4 branch
96104
97105**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
98106
99107- Type:
100108
101109``` bash
102110sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
103111```
104112
105113- Be sure that ppc386 is right installed, type:
106114
107115``` bash
108116which ppc386
109117```
110118
111119if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)
112120
113121### Compile FPC from sources
114122
115123- Type:
116124
117125``` bash
118126cd ~/lazarus/fpc/2.5.1
119127make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
120128sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
121129sudo rm /usr/bin/ppc386
122130sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
123131sudo mkdir /usr/share/fpcsrc/
124132sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
125133sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc
126134```
127135
128136## FPC for ARM
129137
130138### Make FPC able to cross compile for arm-linux
131139
132140- Type:
133141
134142``` bash
135143cd ~/lazarus/fpc/2.5.1/
136144sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr
137145```
138146
139147### Create custom fpc.cfg
140148
141149**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
142150
143151- Type:
144152
145153``` bash
146154gedit ~/.fpc.cfg
147155```
148156
149157- Paste in ~/.fpc.cfg:
150158
151159 #INCLUDE /etc/fpc.cfg
152160 #IFDEF CPUARM
153161 -Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
154162 -Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
155163 -XP/home/user/lazarus/fpc/binutils/
156164 -Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
157165 -Xr/home/user/lazarus/fpc/libcross
158166 -XR/home/user/lazarus/fpc/
159167 -darm
160168 -Tlinux
161169 #ENDIF
162170
163171- save and close gedit
164172- Type:
165173
166174``` bash
167175sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm
168176```
169177
170178## Lazarus
171179
172180If you don't need graphical programs, you can skip this section and go directly to #Appendix B: Target libs
173181
174182### Compile Lazarus
175183
176184- Type:
177185
178186``` bash
179187cd ~/lazarus
180188make clean all
181189```
182190
183191### Start Lazarus
184192
185193- Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
186194- Type:
187195
188196``` bash
189197./lazarus
190198```
191199
192200### Configure Lazarus for cross Compile
193201
194202- 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).
195203- From the IDE:
196204 1. In Tools -\> Options -\> Environment -\> Files, set the Compiler path to the path to fpc. Normally this is already done.
197205 2. Then open Tools / Configure Build Lazarus / Advanced Build Options.
198206 3. Set Target OS as *linux* and Target CPU as *arm*.
199207 4. Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
200208 5. Click the Build button.
201209
202210## Make your first arm-linux project in Lazarus
203211
204212### New Project
205213
206214- Start new project as application
207215- Put some component in main form to test (like button, memo and few other things)
208216
209217### Set Targets
210218
211219- From the IDE:
212220 1. Set in Project / Compiler Options / Code generation / Target OS (-T): *Linux*
213221 2. Set in Project / Compiler Options / Code generation / Target CPU family (-P): *arm*
214222 3. Set in Project / Compiler Options / Other / Custom Option: enter -XParm-linux- into textbox
215223
216224### Add target libs to your project
217225
218226**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
219227
220228- From the IDE:
221229 1. Set in Project / Compiler Options / Linking / Options (-k): *-L/home/user/lazarus/fpc/libcross*
222230 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)
223231
224232### Build Project
225233
226234- Build Project as normal:
227235 1. Hit keys Ctrl+F9
228236 2. Run / Build
229237
230238### Time to run
231239
232240- Transfer the binary of your project on device
233241- Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
234242
235243``` bash
236244chmod +x /path/where/is/YourProject
237245./path/where/is/YourProject
238246```
239247
240248![](/wiki-images/0/08/Kjow_hello-world.png)
241249
242250## APPENDIX A: Testing console programs
243251
244252### Create a TestARM.pas
245253
246254- Type:
247255
248256``` bash
249257mkdir ~/dev/TestARM/
250258gedit ~/dev/TestARM/TestARM.pas
251259```
252260
253261- Paste this code:
254262
255263``` pascal
256264program test;
257265begin
258266 writeln('DATE ',{$i %DATE%});
259267 writeln('FPCTARGET ',{$i %FPCTARGET%});
260268 writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
261269 writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
262270 writeln('FPCVERSION ',{$i %FPCVERSION%});
263271end.
264272```
265273
266274- Type:
267275
268276``` bash
269277cd ~/dev/TestARM/
270278fpc -Tlinux -Parm -XParm-linux- TestARM.pas
271279```
272280
273281### Time to run
274282
275283- Transfer TestARM on device
276284- Generally you must give executable permission to *TestARM* directly on the device:
277285
278286``` bash
279287chmod +x /path/where/is/TestAMR
280288./path/where/is/TestAMR
281289```
282290
283291## Appendix B: Target libs
284292
285293You need target libs to assemble and link binary file. You can find them into the SDK of your device or into the device itself.
286294
287295E.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/\
288296Finally, you need to rename each library *not found* by linker (ld) to the file name searched by linker itself.\
289297E.g. if you got this error
290298
291-`libX11.so not found (or incompatible)`
299+```text
300+libX11.so not found (or incompatible)
301+```
292302
293303and you have a file called *libX11.so.6.2.0*, then you need to rename it into *libX11.so*
294304
295305## See also
296306
297307- [[Android]] Article on setting up cross-compiling on Windows for Android on ARM
298308- [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
299309
300310---
301311*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