Difference between revisions of "Cross compiling for Windows under Linux"

From Free Pascal wiki
Jump to navigationJump to search
(Noted the links to cross compiling binaries appear in /usr/bin not /usr/lib)
(41 intermediate revisions by 16 users not shown)
Line 1: Line 1:
== Hints to Cross Compile Win32 binaries under Linux ==
+
{{Cross compiling for Win32 under Linux}}
  
Since 0.9.10 there is an rpm 'fpc-crosswin32', that installs the needed binutils (e.g. cross assembler, cross linker), the fpc .ppu files cross compiled for win32 and modifies /etc/fpc.cfg.
+
== General ==
  
It does not contain the cross compiled LCL .ppu files. You need to compile them yourself, after installing fpc-crosswin32.
+
=== Introduction - knowing, what you are doing ===
  
== Lazarus and the Free Pascal Compiler ==
+
This is a short introduction for newbies.
 +
The following sections describe how to set up a Linux system to cross compile, creating Win32 executables (or FreeBSD or Darwin/OSX, or ...).
 +
Why cross compiling? Free Pascal is a compiler and basically converts source into binaries (machine language). These binaries also contain information about how the operating system should start the executable. Therefore these binaries are platform specific.
  
=== About Cross compiling - Knowing, what you are doing ===
+
Free Pascal itself does not need much setup. It can create binaries for many platforms. Just tell it to do so. But the compiler is only one part.
  
This is a short introduction for newbies.
+
There is also the assembler and the linker. And these tools are not able to create crossplatform code. That's why we have to create a special linker 'ld' and assembler 'as' for every target platform. These are the binutils.
The following sections describe how to setup a system to cross compile, that means working under linux and creating win32 executables (or freebsd or dawin, or ...).
+
 
Why cross compiling: FreePascal is a compiler and basically converts source into binaries (machine language). These binaries also contains information, how the operating system starts the executable. Therefore these binaries are platform specific.
+
After creating the cross tools, all the FPC Pascal units will be cross compiled. For example, there will then be one system.ppu file for every target platform.  
FreePascal itself does not need much setup. It can create binaries for many platforms. Just tell it to do so. But the compiler is only one part. There is also the assembler and the linker. And these tools are not able to create cross code. That's why we have to create special linker 'ld' and assembler 'as' for every target platform. These are the binutils.
+
Next, your FPC config file (fpc.cfg) will be set up, so that cross compilation becomes so easy, that you can forget all the boring details.
After creating the cross tools, all the fpc pascal units will be cross compiled. For example, then there is one system.ppu file for every target platform.  
+
The same will be done for the LCL - the Lazarus Component Library.
Then your fpc config file will be setup, so that cross compilation becomes so easy, that you can forget all the boring details.
+
And after this you can cross compile Pascal programs for Win32. Either start them with wine or copy them to a Windows machine and test them there.
The same will be done for the LCL - the lazarus component library.
 
And after this you can cross compile pascal programs for win32. Either start them with wine or copy them to a windows machine and test them there.
 
  
=== Why *nix to windows and not the other way around. ===
+
== Free Pascal ==
 +
=== Why *nix 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. Static linking is already complicated, let alone shared.
+
The main reason for this is that generating Linux/Unix binaries on a foreign platform (even another Unix or Linux system) is more complicated. Static linking is already complicated, let alone shared.
  
You would need the used libraries from the target platform's (gtk, glib,libc etc), and a lot of additional configuring for ld. (library paths, dynlinker path etc)
+
You would need the used libraries from the target platform (gtk, glib, libc etc), and a lot of additional configuring for ld (library paths, dynlinker path etc).
  
This has been partially done (for the static case), but it is hard since it needs manual postediting of linker files and linker commandline, and a deep understanding about what makes Unix binaries tick.  
+
This has been partially done (for the static case), but it is hard since it needs manual postediting of linker files and linker commandline, and a deep understanding about what makes Unix binaries tick.
  
=== Download the FPC Sources ===
+
=== Newer FPCs - 2.1.1 and newer ===
 +
If you are compiling a 2.1.1 or newer version of FPC you can just do:
  
The binaries are not enough, you need the complete fpc sources.
+
<syntaxhighlight lang="bash">$ make all OS_TARGET=win32 CPU_TARGET=i386</syntaxhighlight>
See www.freepascal.org. You can use the cvs or a daily snapshot.
+
and then
For the following examples the fpc sources were downloaded to ~/sources/fpc.
+
<syntaxhighlight lang="bash">$ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386"</syntaxhighlight>
  
=== Download the gnu binutils. ===
+
{{Note|<br />make crossinstall by default installs all files in '''/usr/local/lib/fpc/$fpcversion/units''' directory, that's why you will need to add to '''/etc/fpc.cfg file''' a new search path:<br />
 +
'''-Fu/usr/local/lib/fpc/$fpcversion/units/$fpctarget/*'''.<br />
 +
<br />
 +
Another option is to use INSTALL_PREFIX&#61;'''/usr''' while performing crossinstall. In this case, you will not need to change anything in '''/etc/fpc.cfg''' file because all will be installed in '''/usr/lib/fpc/$fpcversion/units''' which is already there}}
  
For example binutils-2.18.tar.gz downloaded to
+
{{Note|<br />To build for win64 the make command is: make all OS_TARGET&#61;win64 CPU_TARGET&#61;x86_64}}
~/download/binutils-2.18.tar.gz.
 
  
=== Cross build binutils ===
+
The reason for this simplicity is the internal linker included in this version of fpc.
  
In the fpcbuild repository there is a script to build the binutils for all
+
=== An example under Bunsen Labs (Debian 8) ===
cross platforms: install/cross/buildcrossbinutils
 
  
Download install of fpcbuild:
+
- Cross-compile to Win32 and Win64 using FPC 3.0.0 and Lazarus 1.6.2
  []$ cd ~
 
  []$ svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install install
 
  
Create a copy of the script:
+
- Open up your terminal and execute the following commands (Many thanks to Handoko and Leledumbo for this, you are awesome).
  
  []$ cd ~/install/cross/
+
<syntaxhighlight lang="bash">
  []$ cp buildcrossbinutils buildcrossbinutils.sh
 
  
Edit the variables at the start of the new script.
+
# Navigate to the fpc source folder.
 +
cd /usr/share/fpcsrc/3.0.0
  
The BASE variable points to a building and installation directory. So, it
+
# Compile the cross-compiler.
should be an empty directory. For example:
+
sudo make clean all OS_TARGET=win32 CPU_TARGET=i386
  BASE=~/cross_fpc
 
  
Now the downloaded binutils file. If for instance you downloaded ~/download/binutils-2.18.tar.gz then set
+
# Install the cross-compiler.
  BINUTILSPATH=~/download/
+
sudo make crossinstall OS_TARGET=win32 CPU_TARGET=i386 INSTALL_PREFIX=/usr
  BINUTILSBASE=binutils
 
  BINUTILSVERSION=2.18
 
  BINUTILS_GZIP=yes
 
  
The script will automatically combine this to ~/download/binutils-2.18.tar.gz.
+
# Link the cross-compiler and place the link where Lazarus can see it.
The rest variables define what target platforms you have. The default is to
+
sudo ln -sf /usr/lib/fpc/3.0.0/ppcross386 /usr/bin/ppcross386
build quite a lot, so compilation will take some time (hours on slow machines).
 
For cross compile to windows, you need only
 
  
  TARGETS_WIN="mingw32"
+
# Do the same using x64 as target
 
+
sudo make clean all OS_TARGET=win64 CPU_TARGET=x86_64
and to comment all other:
+
sudo make crossinstall OS_TARGET=win64 CPU_TARGET=x86_64 INSTALL_PREFIX=/usr
 
+
sudo ln -sf /usr/lib/fpc/3.0.0/ppcrossx64 /usr/bin/ppcrossx64
  #BSD="freebsd netbsd openbsd"
 
  #TARGETS_WIN="cygwin mingw32 msdosdjgpp"
 
  #TARGETS_I386="${BSD} linux solaris darwin"
 
  #TARGETS_POWERPC="${BSD} linux darwin"
 
  #TARGETS_SPARC="${BSD} linux solaris"
 
  #TARGETS_M68k=
 
  
 +
</syntaxhighlight>
  
Then run the script:
+
- Make sure your cross-compilers are alive: (Note, the links to the cross compiling binaries appear in /usr/bin rather than /usr/lib as shown in the images below).
  
  []$ sh buildcrossbinutils.sh
+
[[File:000-cross-compilers-compiled.png]]
 
 
The script creates a subdirectory 'logs' full of log files. If something goes
 
wrong, start looking there.
 
  
Note that for several platforms (Linux,FreeBSD, win32) these are available in compiled
+
- Make sure your cross-compilers were properly linked:
form already. See ftp://freepascal.stack.nl/pub/fpc/contrib/cross/
 
  
=== Cross build FPC ===
+
[[File:000-cross-compilers-linked.png]]
  
In the fpcbuld repository there is a script to build the fpc snapshot for
+
- Now open Lazarus.
all cross platforms: install/cross/buildcrosssnapshot
 
Create a copy of the script:
 
  
  []$ cd ~/install/cross/
+
- Find the "Paths" item in the right list of the "Project Options" window. Press Ctrl+Shift+F11 or navigate through the Projects->Project Options->Paths menus.
  []$ cp buildcrosssnapshot buildcrosssnapshot.sh
 
  
Edit the variables at the start of the new script.
+
- For each build, configure your paths so that all necessary libraries are reacheable and all output files can be generated by Lazarus/FPC with no overriding. I have chosen to use macros in the "Unit output path" and "Target file name".
  
Normally you will change at least CROSSTOOLSROOT, FPCCVS, DESTDIR, TARGETS_OS
+
[[File:002-paths-win32.png]]
and TARGETS_CPU. For example:
 
 
 
  CROSSTOOLSROOT=~/cross_fpc/cross
 
  FPCCVS=~/sources/fpc
 
  TARGETS_OS="win32"
 
  TARGETS_CPU="i386"
 
  DESTDIR=~/cross_fpc/
 
 
 
Then run the script:
 
  
  []$ sh buildcrosssnapshot.sh
+
- Create builds and edit build names in the "Build Mode: [BuildName]" window (click in the upper [...] button to open it).
  
After this you got cross compiled units in ~/cross_fpc/
+
[[File:001-build-modes.png]]
  
=== Configure your fpc.cfg ===
+
- Click ok when your done.
  
Open as root your /etc/fpc.cfg or create copy /etc/fpc.cfg to ~/fpc.cfg and edit this file. Search in the config for the unit search paths
+
- Now go to Run -> "Build Many Modes". Press ok. Wait until Lazarus and FPC finishes their work.  
  
Note: On older versions of FPC $version and $target are used instead of $fpcversion and $fpctarget
+
[[File:003-build-many-modes.png]]
-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
 
-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
 
-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
 
  
Replace them with special paths for other platforms. For example for normal linux and cross compiled win32:
+
- Go to your project folder and enjoy!
#IFDEF win32
 
-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/
 
-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/*
 
-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/rtl
 
-XPi686-mingw32-
 
-FD~/cross_fpc/cross/bin
 
#ELSE linux
 
-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
 
-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
 
-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
 
#-Fu~/fpc/packages/*;~/fpc/rtl/linux
 
#ENDIF
 
  
ToDo: Test and troubleshooting
+
[[File:004-executables.png||||Write once, compile anywhere.]]
  
=== Cross compiling the LCL and lazarus components ===
+
=== FPC older than 2.1.1 ===
 +
For FPC versions older than 2.1.1, please see the History link (version before 19 August 2014) to see the steps needed
  
At Command line:
+
== Lazarus/LCL ==
cd lazarus; make clean all OS_TARGET=win32
 
cd lazarus/lcl; make clean all
 
  
This will first cross compile everything for win32 (including the IDE, which is unecessary, but so I have to write less doc).
+
=== Cross compiling the LCL and Lazarus components ===
  
Or in the IDE: Set LCL, Synedit, Codetools, Package Registration and IDE Interface to Clean+Build, set LCL interface to win32/win64 and set 'Target OS' to win32. Then 'build lazarus'.
+
The IDE automatically cross compiles all used packages when you change the target of your project and build it.
These four parts have splitted output directories, so your linux .ppu/.o files are *not* overwritten and you don't need to recompile them.
 
  
 
=== Cross compiling a project ===
 
=== Cross compiling a project ===
  
Set in Run->Compiler Options->Code the Target OS to 'win32' and in Paths the
+
In Project->Compiler Options->Code, set the Target OS to 'win32' and in "Additions and Overrides" click  Set LCL WidgetType and select win32. That's all. The next time you build, you will create a win32 executable.
'LCL Widget Type' to win32. That's all. The next time you build, you will
+
 
create a win32 executable.
+
The IDE will rescan for win32 units, so that 'Find declaration' and code completion features will now work with the win32 rtl instead of the linux rtl.
The IDE will rescan for win32 units, so that 'Find declaration' and code
+
When you open another project or reopen this project the IDE will automatically switch.
completion features will now work with the win32 rtl instead of the linux rtl.
 
When you open another project or reopen this project the IDE will automatically
 
switch.
 
  
 
=== Hints for Cross compiling and Lazarus ===
 
=== Hints for Cross compiling and Lazarus ===
  
If you create an application/package for multiple targets, you will often do the following: Fix a bug, compile and test it under linux, then compile and test it under win32, .. . Because normally you overwrite your .ppu files, you have to recompile everything, everytime you switch. This is not necessary.
+
If you create an application/package for multiple targets, you will often do the following: Fix a bug, compile and test it under Linux, then compile and test it under win32, .. . Because normally you overwrite your .ppu files, you have to recompile everything, everytime you switch. This is not necessary.
 
The Lazarus IDE supports macros.
 
The Lazarus IDE supports macros.
  
 
Example 1: Cross compiling a project for linux and win32.  
 
Example 1: Cross compiling a project for linux and win32.  
  
Set Run -> Compiler Options -> Paths -> Unit Output directory to $(TargetOS). This macro will be replaced by the value in Code -> TargetOS in lowercase (i.e. "linux" for Linux and "win32" for Win32).  
+
Set Project -> Compiler Options -> Paths -> Unit Output directory to $(TargetOS). This macro will be replaced by the value in Code -> TargetOS in lowercase (i.e. "linux" for Linux and "win32" for Win32).  
 
The output directory is relative to your project directory (the directory where your .lpi is). Create a linux and win32 directory in your project directory.
 
The output directory is relative to your project directory (the directory where your .lpi is). Create a linux and win32 directory in your project directory.
 +
 
When you click on the "Show Options" button at the bottom of the compiler options, you will see a -FElinux/ or -FEwin32/. This option tells the compiler where to write the output (e.g. .ppu/.o files).
 
When you click on the "Show Options" button at the bottom of the compiler options, you will see a -FElinux/ or -FEwin32/. This option tells the compiler where to write the output (e.g. .ppu/.o files).
  
Line 179: Line 134:
 
$(TargetCPU)/$(TargetOS)/$(LCLWidgetType)
 
$(TargetCPU)/$(TargetOS)/$(LCLWidgetType)
 
and create the sub directories for all targets. This path construction is also used by the LCL.
 
and create the sub directories for all targets. This path construction is also used by the LCL.
 
 
  
 
The same can be done for packages.
 
The same can be done for packages.
Line 187: Line 140:
  
 
Lazarus packages are not limited to libraries. They can be used to compile nearly everything. And the IDE automatically recompiles them if needed.  
 
Lazarus packages are not limited to libraries. They can be used to compile nearly everything. And the IDE automatically recompiles them if needed.  
Packages can inherit compiler options. For example: A project that uses a package inherits the output directory of the package. In other words: the output directory of the package is added to unit search path of the project. See in the IDE: Run -> Compiler options -> Inherited.
+
 
Inheritance normally works only one way.
+
Packages can inherit compiler options. For example: A project that uses a package inherits the output directory of the package. In other words: the output directory of the package is added to unit search path of the project. See in the IDE: Project -> Compiler options -> Inherited.
But there are exceptions:
+
 
 +
Inheritance normally works only one way, but there are exceptions:
 
The target platform (OS and CPU) of the project overrides the target for all used packages. That means, if you set the Target OS of the project to "win32" and compile the project, the IDE will check if the used packages need to be recompiled for this Target OS.
 
The target platform (OS and CPU) of the project overrides the target for all used packages. That means, if you set the Target OS of the project to "win32" and compile the project, the IDE will check if the used packages need to be recompiled for this Target OS.
  
Line 201: Line 155:
  
 
So, using the macros saves a lot of time.
 
So, using the macros saves a lot of time.
 
== For Unix (general) ==
 
 
Option -XLA is used to rename library dependencies specified in pascal units. Format is -XLAold=new, to modify ld link option -l<old> to -l<new>.
 
 
Option -XR<sysroot> (recent trunk) that can be used to specify the target system root. It's used for:
 
* adding a prefix to the default added library paths; in the past you used to specify -Xd and these paths manually. E.g. for i386-linux instead of passing /lib, /usr/lib, and /usr/X11R6/lib to ld, it will pass <sysroot>/lib, <sysroot>/usr/lib, and <sysroot>/usr/X11R6/lib to ld.
 
* detecting the C library (linux specific): glibc or uclibc. E.g. for uclibc detection '<sysroot>/lib/ld-uClibc.so.0' is tried.
 
 
== For Linux under Win32 ==
 
Marco vd Voort wrote some [http://www.stack.nl/~marcov/crossnotes.txt crossnotes]. If somebody has time, he can add them to this wiki.
 
 
== FreeBSD to sparc ==
 
 
I managed to crosscompile from x86 to Sparc Solaris 9. However the result doesn't work very well, but here is my cmdline:
 
 
in compiler/ execute:
 
 
  gmake cycle CPU_TARGET=sparc OS_TARGET=solaris CROSSBINUTILPREFIX=solaris-sparc- CROSSOPT='-Xd -Fl~/src/sollib'
 
 
~/src/sollib is a directory that contains:
 
* a set of .o's from /usr/local/gcc-3.3-32bit/lib/gcc-lib/sparc-sun-solaris/3.3
 
* libgcc.a from /usr/local/gcc-3.3-32bit/lib/gcc-lib/sparc-sun-solaris/3.3
 
* a set of lib*.so from /usr/lib: libaio.so libmd5.so libc.so libelf.so librt.so libdl.so libm.so       
 
 
Problem is illustrated by the following binary.
 
 
  Free Pascal Compiler version 2.1.1 [2006/03/17] for sparc
 
  Copyright (c) 1993-2005 by Florian Klaempfl
 
  Target OS: Solaris for SPARC
 
  Compiling system.pp
 
  system.pp(15,1) Fatal: Syntax error, "BEGIN" expected but "identifier UNIT" found
 
 
I suspect wrong .o's are taken.
 
 
== For Mac OS X ==
 
See this [http://www.lazarus.freepascal.org/index.php?name=PNphpBB2&file=viewtopic&p=7222#7222 forum post].
 
  
  
 +
==See also==
 +
* [[Cross compiling]]
 +
* [http://www.stack.nl/~marcov/crossnotes.txt crossnotes] Notes about cross compiling
 +
* [[buildfaq]]
  
==Original Contributors==
+
[[Category:Cross compilation]]
This page has been converted from the epikwiki [http://lazarus-ccr.sourceforge.net/index.php?wiki=CrossCompilingForWin32UnderLinux version].
+
[[Category:FPC]]
 +
[[Category:Lazarus]]

Revision as of 05:45, 29 January 2018

English (en)

General

Introduction - knowing, what you are doing

This is a short introduction for newbies. The following sections describe how to set up a Linux system to cross compile, creating Win32 executables (or FreeBSD or Darwin/OSX, or ...). Why cross compiling? Free Pascal is a compiler and basically converts source into binaries (machine language). These binaries also contain information about how the operating system should start the executable. Therefore these binaries are platform specific.

Free Pascal itself does not need much setup. It can create binaries for many platforms. Just tell it to do so. But the compiler is only one part.

There is also the assembler and the linker. And these tools are not able to create crossplatform code. That's why we have to create a special linker 'ld' and assembler 'as' for every target platform. These are the binutils.

After creating the cross tools, all the FPC Pascal units will be cross compiled. For example, there will then be one system.ppu file for every target platform. Next, your FPC config file (fpc.cfg) will be set up, 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. And after this you can cross compile Pascal programs for Win32. Either start them with wine or copy them to a Windows machine and test them there.

Free Pascal

Why *nix to Windows and not the other way around

The main reason for this is that generating Linux/Unix binaries on a foreign platform (even another Unix or Linux system) is more complicated. Static linking is already complicated, let alone shared.

You would need the used libraries from the target platform (gtk, glib, libc etc), and a lot of additional configuring for ld (library paths, dynlinker path etc).

This has been partially done (for the static case), but it is hard since it needs manual postediting of linker files and linker commandline, and a deep understanding about what makes Unix binaries tick.

Newer FPCs - 2.1.1 and newer

If you are compiling a 2.1.1 or newer version of FPC you can just do:

$ make all OS_TARGET=win32 CPU_TARGET=i386

and then

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

Light bulb  Note:
make crossinstall by default installs all files in /usr/local/lib/fpc/$fpcversion/units directory, that's why you will need to add to /etc/fpc.cfg file a new search path:
-Fu/usr/local/lib/fpc/$fpcversion/units/$fpctarget/*.

Another option is to use INSTALL_PREFIX=/usr while performing crossinstall. In this case, you will not need to change anything in /etc/fpc.cfg file because all will be installed in /usr/lib/fpc/$fpcversion/units which is already there
Light bulb  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.

An example under Bunsen Labs (Debian 8)

- Cross-compile to Win32 and Win64 using FPC 3.0.0 and Lazarus 1.6.2

- Open up your terminal and execute the following commands (Many thanks to Handoko and Leledumbo for this, you are awesome).

# Navigate to the fpc source folder.
cd /usr/share/fpcsrc/3.0.0

# Compile the cross-compiler.
sudo make clean all OS_TARGET=win32 CPU_TARGET=i386

# Install the cross-compiler.
sudo make crossinstall OS_TARGET=win32 CPU_TARGET=i386 INSTALL_PREFIX=/usr

# Link the cross-compiler and place the link where Lazarus can see it.
sudo ln -sf /usr/lib/fpc/3.0.0/ppcross386 /usr/bin/ppcross386

# Do the same using x64 as target
sudo make clean all OS_TARGET=win64 CPU_TARGET=x86_64
sudo make crossinstall OS_TARGET=win64 CPU_TARGET=x86_64 INSTALL_PREFIX=/usr
sudo ln -sf /usr/lib/fpc/3.0.0/ppcrossx64 /usr/bin/ppcrossx64

- Make sure your cross-compilers are alive: (Note, the links to the cross compiling binaries appear in /usr/bin rather than /usr/lib as shown in the images below).

000-cross-compilers-compiled.png

- Make sure your cross-compilers were properly linked:

000-cross-compilers-linked.png

- Now open Lazarus.

- Find the "Paths" item in the right list of the "Project Options" window. Press Ctrl+Shift+F11 or navigate through the Projects->Project Options->Paths menus.

- For each build, configure your paths so that all necessary libraries are reacheable and all output files can be generated by Lazarus/FPC with no overriding. I have chosen to use macros in the "Unit output path" and "Target file name".

002-paths-win32.png

- Create builds and edit build names in the "Build Mode: [BuildName]" window (click in the upper [...] button to open it).

001-build-modes.png

- Click ok when your done.

- Now go to Run -> "Build Many Modes". Press ok. Wait until Lazarus and FPC finishes their work.

003-build-many-modes.png

- Go to your project folder and enjoy!

Write once, compile anywhere.

FPC older than 2.1.1

For FPC versions older than 2.1.1, please see the History link (version before 19 August 2014) to see the steps needed

Lazarus/LCL

Cross compiling the LCL and Lazarus components

The IDE automatically cross compiles all used packages when you change the target of your project and build it.

Cross compiling a project

In Project->Compiler Options->Code, set the Target OS to 'win32' and in "Additions and Overrides" click Set LCL WidgetType and select win32. That's all. The next time you build, you will create a win32 executable.

The IDE will rescan for win32 units, so that 'Find declaration' and code completion features will now work with the win32 rtl instead of the linux rtl. When you open another project or reopen this project the IDE will automatically switch.

Hints for Cross compiling and Lazarus

If you create an application/package for multiple targets, you will often do the following: Fix a bug, compile and test it under Linux, then compile and test it under win32, .. . Because normally you overwrite your .ppu files, you have to recompile everything, everytime you switch. This is not necessary. The Lazarus IDE supports macros.

Example 1: Cross compiling a project for linux and win32.

Set Project -> Compiler Options -> Paths -> Unit Output directory to $(TargetOS). This macro will be replaced by the value in Code -> TargetOS in lowercase (i.e. "linux" for Linux and "win32" for Win32). The output directory is relative to your project directory (the directory where your .lpi is). Create a linux and win32 directory in your project directory.

When you click on the "Show Options" button at the bottom of the compiler options, you will see a -FElinux/ or -FEwin32/. This option tells the compiler where to write the output (e.g. .ppu/.o files).


Example 2: Cross compiling a project for various platforms and widget sets.

Set the Unit output directory to $(TargetCPU)/$(TargetOS)/$(LCLWidgetType) and create the sub directories for all targets. This path construction is also used by the LCL.

The same can be done for packages.

Cross compiling and Lazarus Packages

Lazarus packages are not limited to libraries. They can be used to compile nearly everything. And the IDE automatically recompiles them if needed.

Packages can inherit compiler options. For example: A project that uses a package inherits the output directory of the package. In other words: the output directory of the package is added to unit search path of the project. See in the IDE: Project -> Compiler options -> Inherited.

Inheritance normally works only one way, but there are exceptions: The target platform (OS and CPU) of the project overrides the target for all used packages. That means, if you set the Target OS of the project to "win32" and compile the project, the IDE will check if the used packages need to be recompiled for this Target OS.

For example:

Package A has as output directory: lib/$(TargetOS) Project uses A.

  1. The project is built for linux. The IDE compiles A for linux in <PackageDirOfA>/lib/linux/, then it compiles the project for linux.
  2. The project is built for win32. The IDE compiles A for win32 in <PackageDirOfA>/lib/win32/, then it compiles the project for win32.
  3. The project is built again for linux. The IDE checks A for linux and does not recompile it. Then it compiles the project for linux.

So, using the macros saves a lot of time.


See also