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

From Lazarus wiki
Jump to navigationJump to search
m (Freepascal -> Free Pascal)
(→‎Free Pascal: Remove obsolete FPC < 2.1.1 info and refer to history page)
Line 21: Line 21:
 
=== Why *nix to Windows and not the other way around ===
 
=== Why *nix to Windows and not the other way around ===
  
The main reason for this is that generating Unix binaries on a foreign platform (even another Unix or Linux system) 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 (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).
Line 39: Line 39:
  
 
=== FPC older than 2.1.1 ===
 
=== FPC older than 2.1.1 ===
For FPC versions older than 2.1.1, more steps are needed:
+
For FPC versions older than 2.1.1, please see the History link (version before 19 August 2014) to see the steps needed
==== Download the FPC Sources ====
 
 
 
The binaries are not enough, you need the complete fpc sources.
 
See www.freepascal.org. You can use SVN or a daily snapshot ([http://svn.freepascal.org/svn/fpc/ HERE]).
 
For the following examples the FPC sources were downloaded to ~/sources/fpc.
 
 
 
You can download those files from [http://freepascal.org/down/source/sources.var HERE]
 
 
 
==== Download the gnu binutils ====
 
For example binutils-2.18.tar.gz downloaded to
 
~/download/binutils-2.18.tar.gz.
 
 
 
You can download those files from [http://ftp.gnu.org/gnu/binutils/ HERE]
 
 
 
==== Cross build binutils ====
 
In the fpcbuild repository there is a script to build the binutils for all
 
cross platforms: install/cross/buildcrossbinutils
 
 
 
Download install of fpcbuild, e.g something like (adjust version):
 
<syntaxhighlight lang=bash>
 
cd ~
 
svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_4/install install
 
</syntaxhighlight>
 
 
 
Create a copy of the script:
 
<syntaxhighlight lang=bash>
 
cd ~/install/cross/
 
cp buildcrossbinutils buildcrossbinutils.sh
 
</syntaxhighlight>
 
 
 
Edit the variables at the start of the new script.
 
 
 
The BASE variable points to a building and installation directory. So, it
 
should be an empty directory. For example:
 
  BASE=~/cross_fpc
 
 
 
Now the downloaded binutils file. If for instance you downloaded ~/download/binutils-2.18.tar.gz then set
 
  BINUTILSPATH=~/download/
 
  BINUTILSBASE=binutils
 
  BINUTILSVERSION=2.18
 
  BINUTILS_GZIP=yes
 
 
 
The script will automatically combine this to ~/download/binutils-2.18.tar.gz.
 
 
 
The rest of the variables define what target platforms you have. The default is to
 
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"
 
 
 
and comment out all others:
 
 
 
  #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=
 
 
 
 
 
Then run the script:
 
<syntaxhighlight lang=bash>
 
sh buildcrossbinutils.sh
 
</syntaxhighlight>
 
 
 
The script creates a subdirectory 'logs' full of log files. If something goes
 
wrong, start looking there.
 
 
 
Note that the cross binutils for several platforms (Linux,FreeBSD, win32) these are available in compiled form already. See ftp://freepascal.stack.nl/pub/fpc/contrib/cross/
 
 
 
==== Cross build FPC ====
 
In the fpcbuild repository there is a script to build the fpc snapshot for all cross platforms: install/cross/buildcrosssnapshot
 
Create a copy of the script:
 
 
 
<syntaxhighlight lang=bash>
 
cd ~/install/cross/
 
cp buildcrosssnapshot buildcrosssnapshot.sh
 
</syntaxhighlight>
 
 
 
Edit the variables at the start of the new script.
 
 
 
Normally you will change at least CROSSTOOLSROOT, FPCCVS, DESTDIR, TARGETS_OS
 
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:
 
 
 
<syntaxhighlight lang=bash>
 
sh buildcrosssnapshot.sh
 
</syntaxhighlight>
 
 
 
After this you got cross compiled units in ~/cross_fpc/
 
 
 
==== Configure your fpc.cfg ====
 
 
 
As root, open your /etc/fpc.cfg or copy /etc/fpc.cfg to ~/fpc.cfg and edit this file. Search in the config for the unit search paths.
 
 
 
Note: On older versions of FPC $version and $target are used instead of $fpcversion and $fpctarget
 
-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:
 
#IFDEF win32
 
-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/units/i386-win32/
 
-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/units/i386-win32/*
 
-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/units/i386-win32/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
 
  
 
== Lazarus/LCL ==
 
== Lazarus/LCL ==

Revision as of 10:31, 19 August 2014

Template:Translate

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: 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.

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

At the command line:

cd lazarus; make clean all OS_TARGET=win32 CPU_TARGET=i386
cd lazarus/lcl; make clean all

NOTE: For this to work you also need to add: -Fu/usr/local/lib/fpc/$fpcversion/units/$fpctarget/* to the fpc.cfg in /etc (on ubuntu anyway)

This will first cross compile everything for win32 (including the IDE, which is unecessary, but this way is the easiest to explain).

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'. These four parts have split output directories, so your linux .ppu/.o files are *not* overwritten and you don't need to recompile them.

Cross compiling a project

In Project->Compiler Options->Code, set the Target OS to 'win32' and in Paths the '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. 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