Difference between revisions of "Cross compiling for Win32 under Linux/fr"

From Lazarus wiki
Jump to navigationJump to search
(Traduction partielle de la page qui était en anglais (des adaptations mineures ont été faites))
Line 1: Line 1:
{{Cross compiling for Win32 under Linux}}
+
{{Compilation croisée pour Win32 sous Linux}}
  
== Hints to Cross Compile Win32 binaries under Linux ==
+
== Conseil pour la compilation croisée des binaires Win32 sous 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.
+
Depuis 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.
  
 
It does not contain the cross compiled LCL .ppu files. You need to compile them yourself, after installing fpc-crosswin32.
 
It does not contain the cross compiled LCL .ppu files. You need to compile them yourself, after installing fpc-crosswin32.
  
== Lazarus and the Free Pascal Compiler ==
+
== Lazarus et et le compilateur Free Pascal==
  
=== About Cross compiling - Knowing, what you are doing ===
+
=== A propos de la compilation croisée - Savez vous ce que vous faites ? ===
  
 
This is a short introduction for newbies.
 
This is a short introduction for newbies.
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 ...).  
+
Les sections suivantes décrivent comment configurez un system pour la compilation croisée, cela veut dire sous Linux crée des éxécution Win32 (ou FreeBSD, Darwin, ...).
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.
 
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.
 
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.
 
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.
 
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. ===
+
Pourquoi une compilation croisée ? FreePascal est un compilateur qui convertie basiquement les sources en binaire (language machine). Ces binaires contiennent également des informations sur comment le système d'exploitation (OS) démarre l'application. Donc ces binaires sont spécifiques à une plateforme.
 +
FreePascal lui même n'a pas besoin de beaucoup de réglage. Il peut des binaires pour plusieurs plateforme. Il faut lui dire de le faire. Mais le compilateur n'est qu'une partie. Il y a aussi l'assembleur et le linker ! Et ces outils ne sont pas capable de crée un code multiplateforme. C'est pourquoi nous avons crée un linker (ld) et un assmbleur (as) spécial pour chaque plateforme cible. Ce sont les ''binutils''.
 +
Après avoir crée les outils multiplateforme, toutes les unités de FPC vont être compilée en une version multiplateforme. Par exemple il y a un seul ''system.ppu'' pour toutes les plateformes cibles.
 +
Alors le fichier de configuration de FPC (''fpc.cfg'') va être configurer, c'est pour pourquoi la compilation croisée est tellement facile, mais n'oublions pas les détails enuyeux.
 +
La même chose va être faites pour la LCL (Lazarus Component Library).
  
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.
+
Après avoir fait votre compilation croisée vers Win32 il faudra le tester avec [https://www.winehq.org/ Wine] ou (mieux) sur une machine Windows.
  
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)
+
=== Pourquoi *nix vers Windows et pas l'inverse ? ===
 +
 
 +
La principale raison est que la génération des binaires Unix sur une autre plateforme (Windows, ...) est plus compliqué. Les liaisons statiques étant déjà comliquer, Unix utilise la liaison partagée.
 +
 
 +
Vous auriez besoin d'utiliser les librairies des plateformes cibles (gtk, glib, libc, ...) et beaucoup de configuration pour ''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 ===
+
=== Télécharger les sources de FPC ===
  
The binaries are not enough, you need the complete fpc sources.
+
Les binaires ne sont pas suffisants. Vous aurez besoin de toutes les sources de FPC
See www.freepascal.org. You can use SVN or a daily snapshot.
+
Voir  [http://www.freepascal.org le site principale]. Vous pouvez utiliser un SVN ou une snapshot.
For the following examples the fpc sources were downloaded to ~/sources/fpc.
+
Dans cet exemple les sources ont été télécharger dans ~/sources/fpc.
 +
''(à faire:paquet fpc-source de Debian)''
  
=== Download the gnu binutils. ===
+
=== Télécharger les binutils GNU ===
  
For example binutils-2.18.tar.gz downloaded to
+
Dans l'exemle binutils-2.18.tar.gz a été télécharger dans ~/download/binutils-2.18.tar.gz. '''Surtout ne pas le décompresser !'''
~/download/binutils-2.18.tar.gz.
+
Pour télécharger voir [http://ftp.gnu.org/gnu/binutils/ ici]
  
=== Cross build binutils ===
+
=== les binutils de la compilation croisée ===
  
In the fpcbuild repository there is a script to build the binutils for all
+
Dans le dépôt de fpcbuild il existe un script pour les binutils pour toutes les plateformes: install/cross/buildcrossbinutils
cross platforms: install/cross/buildcrossbinutils
 
  
Download install of fpcbuild:
+
Télécharger et installer fpcbuild
 +
Via SVN:
 
   []$ cd ~
 
   []$ cd ~
 
   []$ svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install install
 
   []$ svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install install
  
Create a copy of the script:
+
Crée une copie du script
 
 
 
   []$ cd ~/install/cross/
 
   []$ cd ~/install/cross/
 
   []$ cp buildcrossbinutils buildcrossbinutils.sh
 
   []$ cp buildcrossbinutils buildcrossbinutils.sh
  
Edit the variables at the start of the new script.
+
Éditer les variables :
  
The BASE variable points to a building and installation directory. So, it
+
BASE pointe le dossier de construction et d'installation. Mettez de préférence un dossier vide Par exemple
should be an empty directory. For example:
 
 
   BASE=~/cross_fpc
 
   BASE=~/cross_fpc
  
Now the downloaded binutils file. If for instance you downloaded ~/download/binutils-2.18.tar.gz then set
+
Maintenant on va spécifier où sont les binutils (ici on suppose qu'ils sont dans l'archive ~/download/binutils-2.18.tar.gz)
 
   BINUTILSPATH=~/download/
 
   BINUTILSPATH=~/download/
 
   BINUTILSBASE=binutils
 
   BINUTILSBASE=binutils
Line 65: Line 66:
 
   BINUTILS_GZIP=yes
 
   BINUTILS_GZIP=yes
  
The script will automatically combine this to ~/download/binutils-2.18.tar.gz.
+
Le script va alors automatiquement utiliser notre archive.
The rest variables define what target platforms you have. The default is to
+
Le reste des variables définissent les plateformes à crée. Par défault il en construit énormément et la compilation durent très longtemps (plusieurs sous les machines lentes).
build quite a lot, so compilation will take some time (hours on slow machines).
+
Pour la compilation croisée Windows vous avez juste besoin de
For cross compile to windows, you need only
 
  
 
   TARGETS_WIN="mingw32"
 
   TARGETS_WIN="mingw32"
 
    
 
    
and to comment all other:
+
Commentez alors les autres cibles
 
    
 
    
 
   #BSD="freebsd netbsd openbsd"
 
   #BSD="freebsd netbsd openbsd"
Line 82: Line 82:
  
  
Then run the script:
+
Alors lancer le script
  
 
   []$ sh buildcrossbinutils.sh
 
   []$ sh buildcrossbinutils.sh
 
    
 
    
The script creates a subdirectory 'logs' full of log files. If something goes
+
Le script va crée un sous dossier ''logs'' qui contient pleins de logs. Utiliser le en cas de problème
wrong, start looking there.
 
  
Note that for several platforms (Linux,FreeBSD, win32) these are available in compiled
+
Notez que pour plusieurs plateformes (Linux,FreeBSD, win32) these are available in compiled
form already. See ftp://freepascal.stack.nl/pub/fpc/contrib/cross/
+
form already. Voir ftp://freepascal.stack.nl/pub/fpc/contrib/cross/
  
 
=== Cross build FPC ===
 
=== Cross build FPC ===

Revision as of 11:36, 18 September 2016

Template:Compilation croisée pour Win32 sous Linux

Conseil pour la compilation croisée des binaires Win32 sous Linux

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

It does not contain the cross compiled LCL .ppu files. You need to compile them yourself, after installing fpc-crosswin32.

Lazarus et et le compilateur Free Pascal

A propos de la compilation croisée - Savez vous ce que vous faites ?

This is a short introduction for newbies. Les sections suivantes décrivent comment configurez un system pour la compilation croisée, cela veut dire sous Linux crée des éxécution Win32 (ou FreeBSD, Darwin, ...).

Pourquoi une compilation croisée ? FreePascal est un compilateur qui convertie basiquement les sources en binaire (language machine). Ces binaires contiennent également des informations sur comment le système d'exploitation (OS) démarre l'application. Donc ces binaires sont spécifiques à une plateforme. FreePascal lui même n'a pas besoin de beaucoup de réglage. Il peut des binaires pour plusieurs plateforme. Il faut lui dire de le faire. Mais le compilateur n'est qu'une partie. Il y a aussi l'assembleur et le linker ! Et ces outils ne sont pas capable de crée un code multiplateforme. C'est pourquoi nous avons crée un linker (ld) et un assmbleur (as) spécial pour chaque plateforme cible. Ce sont les binutils. Après avoir crée les outils multiplateforme, toutes les unités de FPC vont être compilée en une version multiplateforme. Par exemple il y a un seul system.ppu pour toutes les plateformes cibles. Alors le fichier de configuration de FPC (fpc.cfg) va être configurer, c'est pour pourquoi la compilation croisée est tellement facile, mais n'oublions pas les détails enuyeux. La même chose va être faites pour la LCL (Lazarus Component Library).

Après avoir fait votre compilation croisée vers Win32 il faudra le tester avec Wine ou (mieux) sur une machine Windows.

Pourquoi *nix vers Windows et pas l'inverse ?

La principale raison est que la génération des binaires Unix sur une autre plateforme (Windows, ...) est plus compliqué. Les liaisons statiques étant déjà comliquer, Unix utilise la liaison partagée.

Vous auriez besoin d'utiliser les librairies des plateformes cibles (gtk, glib, libc, ...) et beaucoup de configuration pour 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.

Télécharger les sources de FPC

Les binaires ne sont pas suffisants. Vous aurez besoin de toutes les sources de FPC Voir le site principale. Vous pouvez utiliser un SVN ou une snapshot. Dans cet exemple les sources ont été télécharger dans ~/sources/fpc. (à faire:paquet fpc-source de Debian)

Télécharger les binutils GNU

Dans l'exemle binutils-2.18.tar.gz a été télécharger dans ~/download/binutils-2.18.tar.gz. Surtout ne pas le décompresser ! Pour télécharger voir ici

les binutils de la compilation croisée

Dans le dépôt de fpcbuild il existe un script pour les binutils pour toutes les plateformes: install/cross/buildcrossbinutils

Télécharger et installer fpcbuild Via SVN:

 []$ cd ~
 []$ svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install install

Crée une copie du script

 []$ cd ~/install/cross/
 []$ cp buildcrossbinutils buildcrossbinutils.sh

Éditer les variables :

BASE pointe le dossier de construction et d'installation. Mettez de préférence un dossier vide Par exemple

 BASE=~/cross_fpc

Maintenant on va spécifier où sont les binutils (ici on suppose qu'ils sont dans l'archive ~/download/binutils-2.18.tar.gz)

 BINUTILSPATH=~/download/
 BINUTILSBASE=binutils
 BINUTILSVERSION=2.18
 BINUTILS_GZIP=yes

Le script va alors automatiquement utiliser notre archive. Le reste des variables définissent les plateformes à crée. Par défault il en construit énormément et la compilation durent très longtemps (plusieurs sous les machines lentes). Pour la compilation croisée Windows vous avez juste besoin de

 TARGETS_WIN="mingw32"
 

Commentez alors les autres cibles

 #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=


Alors lancer le script

 []$ sh buildcrossbinutils.sh
 

Le script va crée un sous dossier logs qui contient pleins de logs. Utiliser le en cas de problème

Notez que pour plusieurs plateformes (Linux,FreeBSD, win32) these are available in compiled form already. Voir ftp://freepascal.stack.nl/pub/fpc/contrib/cross/

Cross build FPC

In the fpcbuld repository there is a script to build the fpc snapshot for all cross platforms: install/cross/buildcrosssnapshot Create a copy of the script:

 []$ cd ~/install/cross/
 []$ cp buildcrosssnapshot buildcrosssnapshot.sh

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:

 []$ sh buildcrosssnapshot.sh

After this you got cross compiled units in ~/cross_fpc/

Configure your fpc.cfg

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

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/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

Cross compiling the LCL and lazarus components

At Command line:

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

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

Cross compiling a project

Set in Project->Compiler Options->Code 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.

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

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

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 forum post.


Original Contributors

This page has been converted from the epikwiki version.