Cross compiling

From Lazarus wiki
Revision as of 00:46, 25 March 2012 by Vincent (talk | contribs) (Text replace - "Delphi>" to "syntaxhighlight>")



This is a short introduction for newbies. The following sections describe how to setup a system to cross compile, that means creating binaries (executables) for a different platform as the one used for compilation - e.g. working under Linux and creating Win32 executables (or those for FreeBSD or Darwin, etc.). In this case, the platform used for compilation is usually referred to as "host" (Linux in the example above) and the platform where you want to run your created binaries is your "target". FreePascal is a compiler and basically converts source into binaries (machine language). These binaries also contain information on how the operating system starts the executables. Moreover, the binaries refer to the APIs provided by the particular operating system, that's why different implementation of our Run-Time Library is necessary for different operating systems. Therefore these binaries are platform specific. FreePascal itself does not need much setup. It can create binaries for many platforms. Just tell it to do so.

Host and target on the same CPU

FPC is designed so that the distributed compiler can create machine code for a certain CPU (because different CPUs need different machine code) and it knows specific requirements for all supported platforms (operating systems) available on that particular CPU. This means that you can perform cross-compilation with the same compiler used for native compilation as long as you stick to the same CPU.

Host and target on different CPUs

If you need to create binaries for a different CPU, you need a special cross-compiler, i.e. compiler running on the host platform, but able to create machine code for a different CPU (in the case of FPC, such a cross-compiler would be again able to target all supported platforms available on the _target_ CPU). This cross-compiler is then usually stored in the same directory as the native compiler. Such a cross-compiler may be either compiled by yourself, or you can use a ready made distributed cross-compiler provided for some platforms directly by the FPC team (usually platforms mostly used in portable devices like arm-linux or arm-wince, because these are usually not used as host platforms). Fpc binary can then select the right compiler (either the native compiler or the cross-compiler) for the target CPU selected using the -P parameter.

Assembler and linker

The compiler is only one part. We also need the assembler and the linker. FPC provides internal assembler and/or linker for just some platforms, other platforms needs to use external tools for that. And these tools are usually not able to create binaries for different platforms. That's why we have to different special linker 'ld' and assembler 'as' for every target platform. These are the binutils.

Units for target

After creating (or having/installing) the cross tools, one needs FPC RTL and other units compiled for the chosen target platform. For example, every target platform needs a different file system.ppu (System unit), etc. These units may be either compiled using your compiler set up for compilation to the target platform, or you may potentially use officially distributed units compiled (and distributed) with exactly the same FPC version (if available in format useable under the particular host platform).


Then your fpc config file will be setup, so that cross compilation becomes so easy, that you can forget all the boring details. The same will be done for the LCL - the lazarus component library (if using Lazarus). And after this you can cross compile pascal programs for the (different) target platform. The resulting binaries may then be copied to a machine running the target platform, or run under an emulator (e.g. Wine for Win32 binaries under Linux, etc.).

Basic Steps

There are a few common steps involved in crosscompiling that you must do in every case:

  • Have already a FreePascal compiler for the platform you wish to compile from.
  • You need to have the FreePascal source code (except for the special case of having everything prepared by someone else).
  • You need to either build from source or obtain binaries of the cross-binutils that run on the platform you are on and are designed to build programs for your desired target platform.
  • Sometimes you will need some files from the target you are compiling to.

From Linux

To Linux

To be clear, the text below is to compile from linux(x86_64) to linux(i386). Information about targetting Linux running on ARM (e.g. Zaurus) may be found in Setup Cross Compile For ARM.

Chances are that your 64 bit linux distrubution is already capable of compiling 32 bit programs but due to the way the fpc build process is designed there are a couple of things you might have to do.

  • First check if you already have the files i386-linux-ld and i386-linux-as:
 bash $ which i386-linux-ld
 bash $ which i386-linux-as

If you have these files skip to the "Compile FPC" heading.

I did not have these files so I made a couple of scripts:

# name this file /usr/bin/i386-linux-ld
ld -A elf32-i386 $@
# name this file /usr/bin/i386-linux-as
as --32 $@
  • Make them executable:
bash $ chmod +x /usr/bin/i386-linux-as
bash $ chmod +x /usr/bin/i386-linux-ld
  • Compile FPC:
bash $ make all CPU_TARGET=i386


bash $ su -c "make install CPU_TARGET=i386"

That's it. Edit your /etc/fpc.cfg file if needed.

To Windows

Information on cross-compilation with Lazarus may be found in Cross compiling for Win32 under Linux

If you are compiling the 2.1.1 or greater branch of fpc you can just do:

bash $ make all OS_TARGET=win32 CPU_TARGET=i386

and then

bash $ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386"

Note: to build for win64 the make command is: make all OS_TARGET=win64 CPU_TARGET=x86_64

The reason for this simplicity is the internal linker included in this version of fpc.

To Darwin or Mac OS X

This section is out of date! New information about cross-compiling from Linux to Darwin is here.

  • First you need the binutils for the platform you want to compile to. Download odcctools from this site (use the cvs version) and follow their instructions for installing.
  • you need to create a fake root dir like: $HOME/darwinroot copy at least the /System and /Frameworks and /usr directories (you may have to copy more than this) from your Apple or Darwin computer to $HOME/darwinroot
  • now that you have these files make a folder in $HOME/darwinroot called cross. where ever you installed the odcctools you need to make links for the cross tools to be more fpc friendly. there are a bunch of files from odcc tools called powerpc-apple-darwin-* you need to make links (or rename them) so powerpc-apple-darwin-ld becomes powerpc-darwin-ld, do the same for *-ar and *-as.
  • now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.


$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)

type (iirc):

make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"

if that succeded you can install it to whereever you want with:

make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc

now copy the file ./compiler/ppccross somewhere you will be able to find it as it's the compiler you'll need to build powerpc programs

  • configure your /etc/fpc.cfg file.

add a section like this:

#IFDEF powerpc
-k-systemroot $HOME/darwin/cross

whenever you want to crosscompile you have to have ppccross and the symlinks to powerpc-darwin-* in the PATH and you should be able to just do ppccross someprogie.pas and it will create a darwin executable.

I may have missed some things (or most everything) as it's been a while since I did this.

From Windows

To Linux

This is less trivial, there is some info in the buildfaq

To GO32v2

Detailed information may be found in Cross-compilation from Win32 to GO32v2.

To wince

arm-wince describes how to setup a crosscompiler for arm CPU
i386-wince describes how to setup compilingt for i386 CPU (no crosscompiling)

Win32 To Win64

If you are compiling the 2.1.1 or greater branch of fpc you can just do:

$ make all OS_TARGET=win64 CPU_TARGET=x86_64

and then

$ make crossinstall OS_TARGET=win64 CPU_TARGET=x86_64

From Darwin (Mac OS X) i386

from i386 to powerpc

The official FPC installer for Mac OS X/i386 includes a PowerPC cross-compiler and all units necessary to compile PowerPC programs (use ppcppc instead of ppc386 to compile your programs). The instructions below are only necessary if you want to compile and install a new version from svn.

  • Compile FPC:
 $ cd fpc
 $ make all CPU_TARGET=powerpc -j 2

This creates the powerpc cross-compiler compiler (fpc/compiler/ppcrosspcc) and all units. You can install them using the following commands:

 $ sudo make FPC=`pwd`/compiler/ppc386 install CPU_TARGET=powerpc CROSSINSTALL=1
 $ INSTALLED_VERSION=`./compiler/ppc386 -iV`
 $ sudo mv /usr/local/lib/fpc/$INSTALLED_VERSION/ppcrossppc /usr/local/lib/fpc/$INSTALLED_VERSION/ppcppc

Reminder: Universal binaries are created from the individual (i386 and powerpc) binaries using lipo.

To Windows, Linux and others

The package manager fink has packages for crosscompiling to windows, linux, freebsd mainly for Intel Macs, but some for PowerPC Macs, too.

$ fink install fpc-cross-i386-win32


$ fink install fpc-cross-arm-linux

install the crosscompilers.

To compile use these commands:

 fpc -Twin32 FILENAME
 fpc -Parm -Tlinux FILENAME

The current list with fpc 2.4.4 is:


For other platforms (processors and systems) you have to do the setup by yourself. It is basically always the same scheme: First, you need the corresponding binutils and second, the crosscompiler and the run time library. Some more details of the building procedure can be learned from the fink package description files of the crosscompilers from above.

Cross compile FAQ

Why cross compile?

So you can develop a program for one OS/CPU and compile it for another OS/CPU without rebooting or switching computers.

Why Unix to Windows and not the other way around?

The main reason for this is that generating unix binaries on a foreign platform (another unix or even Linux distro included) is more complicated.

The main problems are:

  1. You need libraries on the host computer (the computer you are crosscompiling from)
  2. There is not one such set libraries as a result of more complex and dynamic Unix versioning
  3. A lot more (linker) parameters and tweaking is required, which are hard to explain for a general case.

If you still persist after this warning, please have a look at the crossnotes document mentioned further up, or the buildfaq.

I want more information on building Freepascal. Where is it?

There is a general FAQ in pdf format about how to build and configure FPC: buildfaq

Cross compiling the LCL

Test cross compiler

Test if you have installed the cross compiled fpc correctly:

Create a hello world program test.pas:

program test;
  writeln('DATE ',{$i %DATE%});
  writeln('FPCTARGET ',{$i %FPCTARGET%});
  writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
  writeln('FPCVERSION ',{$i %FPCVERSION%});

And compile it with your source/original platform. Example for x86 Windows:

fpc -Twin32 -Pi386 test.pas

Then test source compiler:


Replace win32 and i386 with your targets. Example for target Windows 64 bit:

fpc -Twin64 -Px86_64 test.pas

Then test cross compiler:


The program fpc is a wrapper that searches the right compiler (e.g. ppcrossx64) for the target and executes it.

If this does not work, your cross compiler was not installed correctly. When this works you can cross compile the LCL.

Cross compiling the LCL

If you are sure your cross compiler works, you can do the actual cross compile.

Using the IDE

Perform the following steps in the Lazarus IDE to do an LCL cross compile: In older IDEs:

  • Set in Environment / Options .. / Environment / Files the Compiler path to the path to fpc. Normally this is already done.
  • Then open Tools / Configure Build Lazarus / Advanced Build Options.
  • Set Target OS (e.g. to Win64) and Target CPU (e.g. to x86_64)
  • Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
  • Click the Build button.

In newer IDEs (at least since 0.9.31):

  • Set in Environment / Options .. / Environment / Files the Compiler path to the path to fpc. Normally this is already done.
  • Then open Tools / Configure Build Lazarus.
  • Click the '...' button next to Profile to build and create a new profile, e.g. named Win64 cross.
  • LCL widget type should be set to the widget type of the build, e.g. win32/win64 for a Windows 64 bit cross compiler.
  • Set Target OS (e.g. to Win64) and Target CPU (e.g. to x86_64)
  • Select Build or, if you have trouble, Clean All + Build
  • Click the Save settings and Build buttons.

The LCL will be cross compiled to the new target. The new ppu files are put into their own directories, so the LCL for your normal OS is untouched.

Command line

Apart from the IDE, the command line also allows you to build a cross compiled LCL. An example: you would cross compile a Windows 64 cross compiler using: First thoroughly clean any 64 bit leftovers. This does not touch your 32 bit environment:

make distclean LCL_PLATFORM=win32 CPU_TARGET=x86_64 OS_TARGET=win64

Then build LCL and its required dependencies. We're using LCL_PLATFORM as that presumably corresponds to the widgetset, which is still Windows 32 even on Windows 64 (the widgetsets are the same).

make packager/registration lazutils lcl LCL_PLATFORM=win32 CPU_TARGET=x86_64 OS_TARGET=win64

As in the previous section, the LCL for your normal OS is untouched.

Cross compiling LCL applications

You first need to cross compile the LCL. See above.

Cross compiling applications means: compiling plus linking. When you have cross compiled the LCL the compilation part is easy. Just set in the compiler options of the IDE the Target OS and Target CPU. The tricky part is the linking. If you cross compile a project you may see something like this:

/usr/local/bin/arm-linux-ld: cannot find -lX11

This means you have to install the graphical libraries of the target system. This has nothing to do with FPC/Lazarus, but with cross compiling a library. Some distributions provides pre compiled packages for this. For example Microsoft provides cross compiled libraries for WinCE for Windows. Under Linux you can install Wine to cross compile from Linux to Windows. Some Linux distributions provide 64bit libraries for 32bit systems.