Friday, June 18, 2010

Cross-compile From Windows To Linux via FPC

Since the past time I wrote this I was only able to compile boring console programs and Free Vision examples. Now I would love to share a detailed tutorial how to compile Linux applications from Windows using CodeTyphon IDE.


Cross-compiling using CodeTyphon:

CodeTyphon IDE from PilotLogic  includes all cross-compilers (NDS,GBA,FreeBSD and so etc. ). It weights more than 200mb,but it is worth to download it since it includes such good components like GLScene,ZEOS Access,AGG and lots of OpenGL,DirectX examples in CodeOcean folder.

  1. After downloading extract the archive and run CodeTyphon_win32.exe .
  2. From menu select CodeTyphon - > Remove and Build ALL (FPC+Lazarus)
It may take a while after program will build you a fresh IDE and FPC from sources. Another step is to build a crosscompiler. From main menu select Cross-Build -> Lazarus32 build cross element 1386-linux .


 Wait a while after you will get all LCL and FPC units compiled for Linux i386 system. After getting done this step now you can run the Lazarus IDE by clicking Lazarus icon in the CodeTyphon Center toolbar.
Lets start some testing :)

You can open a simple Lazarus example or write some small demo program. I made just a simple program with a message box.
Boring tiny test app with just a PNG image and message box.
After you made some demo application go to Project - > Project Options... or simply press Shift+CTRL+F11. In the Compiler Options you need to specify a Libraries (-Fl) path. You need specify a folder where you are going to store Linux libraries with .so extension. Just simply click on the "..." button and press the Browse button in opened window. Make a new folder where you are going to put a Linux libraries and select it.

Now comes the hardest part. To compile a Linux programs you will need some native libraries from it. After trying to copy missing GTK 2 libraries from secondary Linux partition I figured that some libraries were way to new for cross-compiler. Luckily I found a old Kubuntu 6.06 Live DVD. The full list of what libraries you exactly need is available up here. As I remember I copied a Pango 1.0 , ATK 1.0, LibGTK2.0, LibGlib2.0,LibCairo2-dev_2.18.17,LibSelinux,LibPNG12 and LibC6-dev_2.3.6,LibX11-6.1  packages from DVD disk and was also using a Ubuntu 10.04 Linux partition in Libraries path. You may try to search for those packages in old Puppy Linux repositories . Also have in mind that you might need to rename libraries libatk-1.0.so.0.1114.0 -> libatk-1.0.so.0 .

It may take a really while to search all those libraries. Use Debian and Ubuntu packages repositories to find packages containing the required libraries.
No matter a bit distorted PNG the executable compiled on Windows worked just fine on my Linux Mint 9 (Ubuntu 10.04) :) 






 If you bother about huge executable you need to uncheck the one debugging option  which causes to increase the executable size up to 10 times!
Go to Project -> Project Options... . In Opened window select Compiler Options -> Linking and uncheck the Display Line Numbers in Run-time Error Backtraces (-gl). 

Further reading:
http://wiki.lazarus.freepascal.org/Cross_compiling
http://wiki.lazarus.freepascal.org/buildfaq
Cross building for Windows Mobile/WinCE
Build Maemo 5 applications with Lazarus

Tools:
PuppyLinux a small (~100mb) Linux distribution that might be installed into USB thumb drive or CF card and used as Lazarus for Linux development environment.
TOP (Tacky Optimizing Puppy) Linux - similar to Puppy Linux,but includes my favorite XFCE desktop shell.

On the weekend I am going to try the arm-linux cross-compiler for Android software.

No comments: