Sign in Join
Wiki copytemporary

Setup Cross Compile For ARM: what changed

Revision 1 to 2. 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
3131`~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm`
3232
3333Binutils sources:
3434
3535`~/fpc_tools/binutils-2.20/`
3636
3737Lazarus sources:
3838
3939`~/lazarus/`
4040
4141FPC 2.5.1 sources:
4242
4343`~/lazarus/fpc/2.5.1/`
4444
4545### Prepare Target Libs
4646
4747- See "#APPENDIX A: Testing console programs" to know how/where to find target libs
4848- Copy all target libs to ~/lazarus/fpc/libcross
4949
5050### Install dependencies
5151
5252``` bash
5353sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev
5454```
5555
5656## Install & Configure Binutils
5757
5858### Compile binutils for arm-linux
5959
6060- If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
6161- Then type:
6262
6363``` bash
6464cd ~/fpc_tools/binutils-2.20
6565./configure --target=arm-linux --disable-werror
6666make
6767sudo make install
6868mkdir ~/lazarus/fpc/binutils
6969ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
7070ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
7171sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
7272
7373sudo gedit /usr/local/bin/arm-linux-as
7474```
7575
7676- Paste:
7777
7878``` bash
7979#!/bin/sh
8080/usr/local/bin/arm-linux-as_org -meabi=5 $@
8181```
8282
8383**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*.
8484
8585- Save and close gedit
8686- Type:
8787
8888``` bash
8989sudo chmod +x /usr/local/bin/arm-linux-as
9090ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as
9191```
9292
9393## FPC
9494
9595### Install precompiled fpc 2.2.4 branch
9696
9797**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
9898
9999- Type:
100100
101101``` bash
102102sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
103103```
104104
105105- Be sure that ppc386 is right installed, type:
106106
107107``` bash
108108which ppc386
109109```
110110
111111if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)
112112
113113### Compile FPC from sources
114114
115115- Type:
116116
117117``` bash
118118cd ~/lazarus/fpc/2.5.1
119119make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
120120sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
121121sudo rm /usr/bin/ppc386
122122sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
123123sudo mkdir /usr/share/fpcsrc/
124124sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
125125sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc
126126```
127127
128128## FPC for ARM
129129
130130### Make FPC able to cross compile for arm-linux
131131
132132- Type:
133133
134134``` bash
135135cd ~/lazarus/fpc/2.5.1/
136136sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr
137137```
138138
139139### Create custom fpc.cfg
140140
141141**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
142142
143143- Type:
144144
145145``` bash
146146gedit ~/.fpc.cfg
147147```
148148
149149- Paste in ~/.fpc.cfg:
150150
151151 #INCLUDE /etc/fpc.cfg
152152 #IFDEF CPUARM
153153 -Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
154154 -Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
155155 -XP/home/user/lazarus/fpc/binutils/
156156 -Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
157157 -Xr/home/user/lazarus/fpc/libcross
158158 -XR/home/user/lazarus/fpc/
159159 -darm
160160 -Tlinux
161161 #ENDIF
162162
163163- save and close gedit
164164- Type:
165165
166166``` bash
167167sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm
168168```
169169
170170## Lazarus
171171
172172If you don't need graphical programs, you can skip this section and go directly to #Appendix B: Target libs
173173
174174### Compile Lazarus
175175
176176- Type:
177177
178178``` bash
179179cd ~/lazarus
180180make clean all
181181```
182182
183183### Start Lazarus
184184
185185- Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
186186- Type:
187187
188188``` bash
189189./lazarus
190190```
191191
192192### Configure Lazarus for cross Compile
193193
194194- 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).
195195- From the IDE:
196196 1. In Tools -\> Options -\> Environment -\> Files, set the Compiler path to the path to fpc. Normally this is already done.
197197 2. Then open Tools / Configure Build Lazarus / Advanced Build Options.
198198 3. Set Target OS as *linux* and Target CPU as *arm*.
199199 4. Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
200200 5. Click the Build button.
201201
202202## Make your first arm-linux project in Lazarus
203203
204204### New Project
205205
206206- Start new project as application
207207- Put some component in main form to test (like button, memo and few other things)
208208
209209### Set Targets
210210
211211- From the IDE:
212212 1. Set in Project / Compiler Options / Code generation / Target OS (-T): *Linux*
213213 2. Set in Project / Compiler Options / Code generation / Target CPU family (-P): *arm*
214214 3. Set in Project / Compiler Options / Other / Custom Option: enter -XParm-linux- into textbox
215215
216216### Add target libs to your project
217217
218218**REMEMBER TO CHANGE EVERY**/home/user/**WITH YOUR OWN USER PATH!**
219219
220220- From the IDE:
221221 1. Set in Project / Compiler Options / Linking / Options (-k): *-L/home/user/lazarus/fpc/libcross*
222222 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)
223223
224224### Build Project
225225
226226- Build Project as normal:
227227 1. Hit keys Ctrl+F9
228228 2. Run / Build
229229
230230### Time to run
231231
232232- Transfer the binary of your project on device
233233- Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
234234
235235``` bash
236236chmod +x /path/where/is/YourProject
237237./path/where/is/YourProject
238238```
239239
240-<figure>
241240<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>
244241
245242## APPENDIX A: Testing console programs
246243
247244### Create a TestARM.pas
248245
249246- Type:
250247
251248``` bash
252249mkdir ~/dev/TestARM/
253250gedit ~/dev/TestARM/TestARM.pas
254251```
255252
256253- Paste this code:
257254
258255``` pascal
259256program test;
260257begin
261258 writeln('DATE ',{$i %DATE%});
262259 writeln('FPCTARGET ',{$i %FPCTARGET%});
263260 writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
264261 writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
265262 writeln('FPCVERSION ',{$i %FPCVERSION%});
266263end.
267264```
268265
269266- Type:
270267
271268``` bash
272269cd ~/dev/TestARM/
273270fpc -Tlinux -Parm -XParm-linux- TestARM.pas
274271```
275272
276273### Time to run
277274
278275- Transfer TestARM on device
279276- Generally you must give executable permission to *TestARM* directly on the device:
280277
281278``` bash
282279chmod +x /path/where/is/TestAMR
283280./path/where/is/TestAMR
284281```
285282
286283## Appendix B: Target libs
287284
288285You need target libs to assemble and link binary file. You can find them into the SDK of your device or into the device itself.
289286
290287E.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/\
291288Finally, you need to rename each library *not found* by linker (ld) to the file name searched by linker itself.\
292289E.g. if you got this error
293290
294291`libX11.so not found (or incompatible)`
295292
296293and you have a file called *libX11.so.6.2.0*, then you need to rename it into *libX11.so*
297294
298295## See also
299296
300297- [[Android]] Article on setting up cross-compiling on Windows for Android on ARM
301298- [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
302299
303300---
304301*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