Difference between revisions of "Setup Cross Compile For ARM"

From Free Pascal wiki
Jump to navigationJump to search
m (→‎See also: clarified description)
 
(36 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Translate}}<!--{{Setup Cross Compile For ARM}}-->
+
{{Setup Cross Compile For ARM}}
  
 
== How To setup Lazarus/FPC for arm-linux cross compiling. ==
 
== How To setup Lazarus/FPC for arm-linux cross compiling. ==
  
 
With this tutorial you can make a binary file that runs on arm-linux devices. This is tested with a Nokia N900 with "[http://maemo.org/ Maemo 5 OS]" (the gnu/linux Debian based OS, developed by [http://maemo.nokia.com/videos/introducing-maemo-5/ Nokia]. It is an arm-linux device that runs GTK2 and QT4.6).
 
With this tutorial you can make a binary file that runs on arm-linux devices. This is tested with a Nokia N900 with "[http://maemo.org/ Maemo 5 OS]" (the gnu/linux Debian based OS, developed by [http://maemo.nokia.com/videos/introducing-maemo-5/ Nokia]. It is an arm-linux device that runs GTK2 and QT4.6).
If you need a simply "command prompt"/"terminal" program, after [[#FPC for ARM]] section skip to [[#APPENDIX A: Testing console programs]].
 
  
'''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 at today.
+
If you need a simple "command prompt"/"terminal" program, after [[#FPC for ARM]] section skip to [[#APPENDIX A: Testing console programs]].
  
'''NOTE 2''': I don't know if this tutorial follows the better way to do all things... I'm not big expert about linux dev or commands, so please help me to improve this guide! You can discuss and repost [http://forum.lazarus.freepascal.org/index.php/topic,8084.0.html HERE]
+
'''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.
  
'''NOTE 3''': Replace every ''/home/user/'' with your real user path. All other paths are simply for explanation. If you use "[http://maemovmware.garage.maemo.org/2nd_edition/ Maemo SDK Virtual Image]", replace every ''/home/user/'' with ''/home/maemo/''
+
'''NOTE 2''': Replace every ''/home/user/'' with your real user path. All other paths are simply for explanation. If you use "[http://maemovmware.garage.maemo.org/2nd_edition/ Maemo SDK Virtual Image]", replace every ''/home/user/'' with ''/home/maemo/''
 
 
 
 
----
 
  
 
== Automated Script ==
 
== Automated Script ==
 +
The previously provided scripts are obsolete. Please see [http://wiki.lazarus.freepascal.org/Lazarus_Manager HERE] (see discussion  page for alternative.
  
For those want things simply, I made a script ('''lazarus_first_time''') that make all things described in this page.<br>
+
== Generic things to do ==
It downloads svn sources and zip needed, build and configure your Ubuntu (x86) box.
 
 
 
You had just to download it somewhere (like Desktop) and simply launch it from terminal with '''sh''', e.g.:
 
 
 
user@user-desktop:~/Desktop$ sh lazarus_first_time
 
********************************************************
 
*                                                      *
 
*                Automated Installation                *
 
*    for install Lazarus/FPC with arm cross compile    *
 
*                                      Script by Kjow *
 
*                                                      *
 
* This script is designed for Ubuntu X86 (32 Bit)      *
 
*                                                      *
 
********************************************************
 
 
Please, enter your account user:
 
user
 
 
 
For daily/instantly svn updates there is an other script ('''lazarus_update''') that updates and rebuilds:
 
 
 
user@user-desktop:~/Desktop$ sh lazarus_update
 
********************************************************
 
*                                                      *
 
*                  Automated Update                  *
 
*          for update Lazarus/FPC/GLScene svn          *
 
*                                      Script by Kjow *
 
*                                                      *
 
* This script is designed for Ubuntu X86 (32 Bit)      *
 
*                                                      *
 
********************************************************
 
 
Please, enter your account user:
 
user
 
...
 
  
You can download those files from [http://forum.lazarus.freepascal.org/index.php/topic,8084.0.html HERE] (see attachment of first post)
+
{{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.}}
 
 
== Generic things to do ==
 
  
 
=== Downloads ===
 
=== Downloads ===
*any starting compiler from the 2.2.4 branch (e.g. fpc-2.2.4-20091214.i386.rpm) from [http://www.hu.freepascal.org/lazarus HERE] or [http://snapshots.lazarus.shikami.org/ HERE]). It is needed to compile >= 2.2.4 fpc sources
+
* any starting compiler from the 2.2.4 branch (e.g. fpc-2.2.4-20091214.i386.rpm) from [http://www.hu.freepascal.org/lazarus HERE] or [http://snapshots.lazarus.shikami.org/ HERE]). It is needed to compile >= 2.2.4 fpc sources
*latest binutils.tar.gz (e.g. binutils-2.20.tar.gz) from [http://ftp.gnu.org/gnu/binutils/ HERE]
+
* latest binutils.tar.gz (e.g. binutils-2.20.tar.gz) from [http://ftp.gnu.org/gnu/binutils/ HERE]
*latest [http://ftp//ftp.freepascal.org/pub/fpc/snapshot/trunk/source/fpc.zip fpc.zip] from [http://www.freepascal.org/develop.var HERE]
+
* latest [http://ftp//ftp.freepascal.org/pub/fpc/snapshot/trunk/source/fpc.zip fpc.zip] from [http://www.freepascal.org/develop.var HERE]
*latest Lazarus source zip (e.g. lazarus-0.9.29-23129-20091214-src.zip) from [http://www.hu.freepascal.org/lazarus HERE] or [http://snapshots.lazarus.shikami.org/ HERE] (faster for me)
+
* latest Lazarus source zip (e.g. lazarus-0.9.29-23129-20091214-src.zip) from [http://www.hu.freepascal.org/lazarus HERE] or [http://snapshots.lazarus.shikami.org/ HERE] (faster for me)
*target libs (You can grab from SDK or from arm-device. ''See "[[#APPENDIX A: Testing console programs]]"'')
+
* target libs (You can grab from SDK or from arm-device. ''See "[[#APPENDIX A: Testing console programs]]"'')
  
 
=== Unpack/copy all files in the respective directories ===
 
=== Unpack/copy all files in the respective directories ===
Line 75: Line 36:
  
 
=== Prepare Target Libs ===
 
=== Prepare Target Libs ===
*See "[[#APPENDIX A: Testing console programs]]" to know how/where to find target libs
+
* See "[[#APPENDIX A: Testing console programs]]" to know how/where to find target libs
*Copy all target libs to ~/lazarus/fpc/libcross
+
* Copy all target libs to ~/lazarus/fpc/libcross
  
 
=== Install dependencies ===
 
=== Install dependencies ===
  sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev
+
<syntaxhighlight lang="bash">sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev</syntaxhighlight>
  
 
== Install & Configure Binutils ==
 
== Install & Configure Binutils ==
  
 
=== Compile binutils for arm-linux ===
 
=== Compile binutils for arm-linux ===
*If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
+
* If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
*Then type:
+
* Then type:
cd ~/fpc_tools/binutils-2.20
+
<syntaxhighlight lang="bash">
./configure --target=arm-linux --disable-werror
+
cd ~/fpc_tools/binutils-2.20
make
+
./configure --target=arm-linux --disable-werror
sudo make install
+
make
mkdir ~/lazarus/fpc/binutils
+
sudo make install
ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
+
mkdir ~/lazarus/fpc/binutils
ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
+
ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
+
ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
 +
sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
  
sudo gedit /usr/local/bin/arm-linux-as
+
sudo gedit /usr/local/bin/arm-linux-as
 +
</syntaxhighlight>
  
*Paste:
+
* Paste:
  
#!/bin/sh
+
<syntaxhighlight lang="bash">#!/bin/sh
/usr/local/bin/arm-linux-as_org -meabi=5 $@
+
/usr/local/bin/arm-linux-as_org -meabi=5 $@</syntaxhighlight>
  
 
'''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''.
 
'''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''.
  
*Save and close gedit
+
* Save and close gedit
*Type:
+
* Type:
  
sudo chmod +x /usr/local/bin/arm-linux-as
+
<syntaxhighlight lang="bash">
ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as
+
sudo chmod +x /usr/local/bin/arm-linux-as
 +
ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as
 +
</syntaxhighlight>
  
 
== FPC ==
 
== FPC ==
Line 114: Line 79:
 
=== Install precompiled fpc 2.2.4 branch ===
 
=== Install precompiled fpc 2.2.4 branch ===
 
'''REMEMBER TO CHANGE EVERY '''/home/user/''' WITH YOUR OWN USER PATH!'''
 
'''REMEMBER TO CHANGE EVERY '''/home/user/''' WITH YOUR OWN USER PATH!'''
*Type:
+
* Type:
sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
+
<syntaxhighlight lang="bash">sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm</syntaxhighlight>
*Be sure that ppc386 is right installed, type:
+
* Be sure that ppc386 is right installed, type:
which ppc386
+
<syntaxhighlight lang="bash">which ppc386</syntaxhighlight>
 
if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)
 
if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)
  
 
=== Compile FPC from sources ===
 
=== Compile FPC from sources ===
*Type:
+
* Type:
cd ~/lazarus/fpc/2.5.1
+
<syntaxhighlight lang="bash">
make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
+
cd ~/lazarus/fpc/2.5.1
sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
+
make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
sudo rm /usr/bin/ppc386
+
sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
+
sudo rm /usr/bin/ppc386
sudo mkdir /usr/share/fpcsrc/
+
sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
+
sudo mkdir /usr/share/fpcsrc/
sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc
+
sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
 +
sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc
 +
</syntaxhighlight>
 +
 
 
== FPC for ARM ==
 
== FPC for ARM ==
 +
 
=== Make FPC able to cross compile for arm-linux ===
 
=== Make FPC able to cross compile for arm-linux ===
*Type:
+
* Type:
cd ~/lazarus/fpc/2.5.1/
+
<syntaxhighlight lang="bash">
sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr
+
cd ~/lazarus/fpc/2.5.1/
 +
sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr
 +
</syntaxhighlight>
 +
 
 
=== Create custom fpc.cfg ===
 
=== Create custom fpc.cfg ===
 
'''REMEMBER TO CHANGE EVERY '''/home/user/''' WITH YOUR OWN USER PATH!'''
 
'''REMEMBER TO CHANGE EVERY '''/home/user/''' WITH YOUR OWN USER PATH!'''
*Type:
+
* Type:
gedit ~/.fpc.cfg
+
<syntaxhighlight lang="bash">gedit ~/.fpc.cfg</syntaxhighlight>
*Paste in ~/.fpc.cfg:
+
* Paste in ~/.fpc.cfg:
#INCLUDE /etc/fpc.cfg
+
<pre>
#DEFINE DEMOTEST
+
#INCLUDE /etc/fpc.cfg
#DEFINE DEMOTEST1
+
#IFDEF CPUARM
#DEFINE LAZARUS
+
-Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
+
-Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
-Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
+
-XP/home/user/lazarus/fpc/binutils/
-Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
+
-Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
+
-Xr/home/user/lazarus/fpc/libcross
-a
+
-XR/home/user/lazarus/fpc/
-Sd
+
-darm
-Xd
+
-Tlinux
-Xs
+
#ENDIF
+
</pre>
-O-
 
 
#IFDEF CPUARM
 
-XP/home/user/lazarus/fpc/binutils/
 
-Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
 
-Xr/home/user/lazarus/fpc/libcross
 
-XR/home/user/lazarus/fpc/
 
-darm
 
-Tlinux
 
#ENDIF
 
  
*save and close gedit
+
* save and close gedit
*Type:
+
* Type:
sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm
+
<syntaxhighlight lang="bash">sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm</syntaxhighlight>
  
 
== Lazarus ==
 
== Lazarus ==
 
If you don't need graphical programs, you can skip this section and go directly to [[#Appendix B: Target libs]]
 
If you don't need graphical programs, you can skip this section and go directly to [[#Appendix B: Target libs]]
 +
 
=== Compile Lazarus ===
 
=== Compile Lazarus ===
*Type:
+
* Type:
cd ~/lazarus
+
<syntaxhighlight lang="bash">
make clean all
+
cd ~/lazarus
 +
make clean all
 +
</syntaxhighlight>
 +
 
 
=== Start Lazarus ===
 
=== Start Lazarus ===
*Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
+
* Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
*Type:
+
* Type:
./lazarus
+
<syntaxhighlight lang="bash">./lazarus</syntaxhighlight>
 +
 
 
=== Configure Lazarus for cross Compile ===
 
=== Configure Lazarus for cross Compile ===
*Now you must cross compile the LCL and Package Registration, this part come from [http://wiki.lazarus.freepascal.org/Cross_compiling#Cross_compiling_the_LCL HERE].
+
* Now you must cross compile the LCL and Package Registration, this part comes from [http://wiki.lazarus.freepascal.org/Cross_compiling#Cross_compiling_the_LCL HERE].
*From the IDE:
+
* From the IDE:
*#Set in Environment / Options .. / Environment / Files the Compiler path to the path to fpc. Normally this is already done.
+
*# In Tools -> Options -> Environment -> Files, set the Compiler path to the path to fpc. Normally this is already done.
*#Then open Tools / Configure Build Lazarus / Advanced Build Options.
+
*# Then open Tools / Configure Build Lazarus / Advanced Build Options.
*#Set Target OS as linux and Target CPU as arm ().
+
*# Set Target OS as ''linux'' and Target CPU as ''arm''.
*#Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
+
*# Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
*#Click the Build button.
+
*# Click the Build button.
  
 
== Make your first arm-linux project in Lazarus ==
 
== Make your first arm-linux project in Lazarus ==
 +
 
=== New Project ===
 
=== New Project ===
*Start new project as application
+
* Start new project as application
*Put some component in main form to test (like button, memo and few other things)
+
* Put some component in main form to test (like button, memo and few other things)
 +
 
 
=== Set Targets ===
 
=== Set Targets ===
*From the IDE:
+
* From the IDE:
*#Set in Project / Compiler Options / Code / Target OS (-T): ''Linux''
+
*# Set in Project / Compiler Options / Code generation / Target OS (-T): ''Linux''
*#Set in Project / Compiler Options / Code / Target CPU family (-P): ''arm''
+
*# Set in Project / Compiler Options / Code generation / Target CPU family (-P): ''arm''
 +
*# Set in Project / Compiler Options / Other / Custom Option: enter -XParm-linux- into textbox
 +
 
 
=== Add target libs to your project ===
 
=== Add target libs to your project ===
*From the IDE:
+
'''REMEMBER TO CHANGE EVERY '''/home/user/''' WITH YOUR OWN USER PATH!'''
*#Set in Project / Compiler Options / Linking / Options (-k): ''-L/home/user/lazarus/fpc/libcross''
+
* From the IDE:
*#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)
+
*# Set in Project / Compiler Options / Linking / Options (-k): ''-L/home/user/lazarus/fpc/libcross''
 +
*# 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)
  
 
=== Build Project ===
 
=== Build Project ===
*Build Project as normal:
+
* Build Project as normal:
*#Hit keys Ctrl+F9
+
*# Hit keys Ctrl+F9
*#Run / Build
+
*# Run / Build
 +
 
 
=== Time to run ===
 
=== Time to run ===
*Transfer the binary of your project on device
+
* Transfer the binary of your project on device
*Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
+
* Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
chmod +x /path/where/is/YourProject
+
<syntaxhighlight lang="bash">
./path/where/is/YourProject
+
chmod +x /path/where/is/YourProject
 +
./path/where/is/YourProject
 +
</syntaxhighlight>
  
 
[[Image:Kjow hello-world.png|thumb|300px|center|alt=Nokia N900|e.g. Maemo 5 running Lazarus graphical demo.]]
 
[[Image:Kjow hello-world.png|thumb|300px|center|alt=Nokia N900|e.g. Maemo 5 running Lazarus graphical demo.]]
Line 215: Line 190:
  
 
=== Create a TestARM.pas ===
 
=== Create a TestARM.pas ===
*Type:
+
 
mkdir ~/dev/TestARM/
+
* Type:
gedit ~/dev/TestARM/TestARM.pas
+
 
*Paste this code:
+
<syntaxhighlight lang="bash">
program test;
+
mkdir ~/dev/TestARM/
begin
+
gedit ~/dev/TestARM/TestARM.pas
 +
</syntaxhighlight>
 +
 
 +
* Paste this code:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
program test;
 +
begin
 
   writeln('DATE ',{$i %DATE%});
 
   writeln('DATE ',{$i %DATE%});
 
   writeln('FPCTARGET ',{$i %FPCTARGET%});
 
   writeln('FPCTARGET ',{$i %FPCTARGET%});
Line 226: Line 208:
 
   writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
 
   writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
 
   writeln('FPCVERSION ',{$i %FPCVERSION%});
 
   writeln('FPCVERSION ',{$i %FPCVERSION%});
end.
+
end.
*Type:
+
</syntaxhighlight>
cd ~/dev/TestARM/
+
 
fpc -Tlinux -Pi386 TestARM.pas
+
* Type:
 +
 
 +
<syntaxhighlight lang="bash">
 +
cd ~/dev/TestARM/
 +
fpc -Tlinux -Parm -XParm-linux- TestARM.pas
 +
</syntaxhighlight>
  
 
=== Time to run ===
 
=== Time to run ===
*Transfer TestARM on device
+
 
*Generally you must give executable permission to ''TestARM'' directly on the device:
+
* Transfer TestARM on device
chmod +x /path/where/is/TestAMR
+
* Generally you must give executable permission to ''TestARM'' directly on the device:
./path/where/is/TestAMR
+
 
 +
<syntaxhighlight lang="bash">
 +
chmod +x /path/where/is/TestAMR
 +
./path/where/is/TestAMR
 +
</syntaxhighlight>
  
 
== Appendix B: Target libs ==
 
== Appendix B: Target libs ==
Line 243: Line 234:
 
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/<br>
 
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/<br>
 
Finally, you need to rename each library ''not found'' by linker (ld) to the file name searched by linker itself.<br>
 
Finally, you need to rename each library ''not found'' by linker (ld) to the file name searched by linker itself.<br>
I mean, if you got this error
+
 
 +
E.g. if you got this error
  
 
  libX11.so not found (or incompatible)
 
  libX11.so not found (or incompatible)
  
and you have a file called ''libX11.so.6.2.0'' you need to rename it into ''libX11.so''
+
and you have a file called ''libX11.so.6.2.0'', then you need to rename it into ''libX11.so''
 
 
  
 +
== See also ==
  
[[User:Kjow|Kjow]] 18:42, 16 December 2009 (CET)<br>
+
* [[Android]] Article on setting up cross-compiling on Windows for Android on ARM
Latest Revision: [[User:Kjow|Kjow]] 11:20, 23 March 2010 (CET)
+
* [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.

Latest revision as of 01:45, 26 November 2020

English (en) español (es) magyar (hu) Bahasa Indonesia (id) русский (ru)

How To setup Lazarus/FPC for arm-linux cross compiling.

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" (the gnu/linux Debian based OS, developed by Nokia. It is an arm-linux device that runs GTK2 and QT4.6).

If you need a simple "command prompt"/"terminal" program, after #FPC for ARM section skip to #APPENDIX A: Testing console programs.

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.

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", replace every /home/user/ with /home/maemo/

Automated Script

The previously provided scripts are obsolete. Please see HERE (see discussion page for alternative.

Generic things to do

Warning-icon.png

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.

Downloads

  • any starting compiler from the 2.2.4 branch (e.g. fpc-2.2.4-20091214.i386.rpm) from HERE or HERE). It is needed to compile >= 2.2.4 fpc sources
  • latest binutils.tar.gz (e.g. binutils-2.20.tar.gz) from HERE
  • latest fpc.zip from HERE
  • latest Lazarus source zip (e.g. lazarus-0.9.29-23129-20091214-src.zip) from HERE or HERE (faster for me)
  • target libs (You can grab from SDK or from arm-device. See "#APPENDIX A: Testing console programs")

Unpack/copy all files in the respective directories

FPC 2.2.4 installer:

~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm

Binutils sources:

~/fpc_tools/binutils-2.20/

Lazarus sources:

~/lazarus/

FPC 2.5.1 sources:

~/lazarus/fpc/2.5.1/

Prepare Target Libs

Install dependencies

sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev

Install & Configure Binutils

Compile binutils for arm-linux

  • If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
  • Then type:
cd ~/fpc_tools/binutils-2.20
./configure --target=arm-linux --disable-werror
make
sudo make install
mkdir ~/lazarus/fpc/binutils
ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org

sudo gedit /usr/local/bin/arm-linux-as
  • Paste:
#!/bin/sh
/usr/local/bin/arm-linux-as_org -meabi=5 $@

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.

  • Save and close gedit
  • Type:
sudo chmod +x /usr/local/bin/arm-linux-as
ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as

FPC

Install precompiled fpc 2.2.4 branch

REMEMBER TO CHANGE EVERY /home/user/ WITH YOUR OWN USER PATH!

  • Type:
sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
  • Be sure that ppc386 is right installed, type:
which ppc386

if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)

Compile FPC from sources

  • Type:
cd ~/lazarus/fpc/2.5.1
make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
sudo rm /usr/bin/ppc386
sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
sudo mkdir /usr/share/fpcsrc/
sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc

FPC for ARM

Make FPC able to cross compile for arm-linux

  • Type:
cd ~/lazarus/fpc/2.5.1/
sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr

Create custom fpc.cfg

REMEMBER TO CHANGE EVERY /home/user/ WITH YOUR OWN USER PATH!

  • Type:
gedit ~/.fpc.cfg
  • Paste in ~/.fpc.cfg:
#INCLUDE /etc/fpc.cfg
#IFDEF CPUARM
-Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
-Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
-XP/home/user/lazarus/fpc/binutils/
-Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
-Xr/home/user/lazarus/fpc/libcross
-XR/home/user/lazarus/fpc/
-darm
-Tlinux
#ENDIF
  • save and close gedit
  • Type:
sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm

Lazarus

If you don't need graphical programs, you can skip this section and go directly to #Appendix B: Target libs

Compile Lazarus

  • Type:
cd ~/lazarus
make clean all

Start Lazarus

  • Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
  • Type:
./lazarus

Configure Lazarus for cross Compile

  • Now you must cross compile the LCL and Package Registration, this part comes from HERE.
  • From the IDE:
    1. In Tools -> Options -> Environment -> Files, set the Compiler path to the path to fpc. Normally this is already done.
    2. Then open Tools / Configure Build Lazarus / Advanced Build Options.
    3. Set Target OS as linux and Target CPU as arm.
    4. Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
    5. Click the Build button.

Make your first arm-linux project in Lazarus

New Project

  • Start new project as application
  • Put some component in main form to test (like button, memo and few other things)

Set Targets

  • From the IDE:
    1. Set in Project / Compiler Options / Code generation / Target OS (-T): Linux
    2. Set in Project / Compiler Options / Code generation / Target CPU family (-P): arm
    3. Set in Project / Compiler Options / Other / Custom Option: enter -XParm-linux- into textbox

Add target libs to your project

REMEMBER TO CHANGE EVERY /home/user/ WITH YOUR OWN USER PATH!

  • From the IDE:
    1. Set in Project / Compiler Options / Linking / Options (-k): -L/home/user/lazarus/fpc/libcross
    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)

Build Project

  • Build Project as normal:
    1. Hit keys Ctrl+F9
    2. Run / Build

Time to run

  • Transfer the binary of your project on device
  • Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
chmod +x /path/where/is/YourProject
./path/where/is/YourProject
Nokia N900
e.g. Maemo 5 running Lazarus graphical demo.

APPENDIX A: Testing console programs

Create a TestARM.pas

  • Type:
mkdir ~/dev/TestARM/
gedit ~/dev/TestARM/TestARM.pas
  • Paste this code:
program test;
begin
  writeln('DATE ',{$i %DATE%});
  writeln('FPCTARGET ',{$i %FPCTARGET%});
  writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
  writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
  writeln('FPCVERSION ',{$i %FPCVERSION%});
end.
  • Type:
cd ~/dev/TestARM/
fpc -Tlinux -Parm -XParm-linux- TestARM.pas

Time to run

  • Transfer TestARM on device
  • Generally you must give executable permission to TestARM directly on the device:
chmod +x /path/where/is/TestAMR
./path/where/is/TestAMR

Appendix B: Target libs

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.

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/
Finally, you need to rename each library not found by linker (ld) to the file name searched by linker itself.

E.g. if you got this error

libX11.so not found (or incompatible)

and you have a file called libX11.so.6.2.0, then you need to rename it into libX11.so

See also

  • Android Article on setting up cross-compiling on Windows for Android on ARM
  • Cross compiling for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.