Sign in Join
Wiki copytemporary

Cross compiling for Win32 under Linux/fr: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Conseil pour la compilation croisée des binaires Win32 sous Linux
22
33Depuis 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.
44
55It does not contain the cross compiled LCL .ppu files. You need to compile them yourself, after installing fpc-crosswin32.
66
77## Lazarus et et le compilateur Free Pascal
88
99### A propos de la compilation croisée - Savez vous ce que vous faites ?
1010
1111C'est une petite introduction pour les nouveaux. 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, ...).
1212
1313Pourquoi 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).
1414
1515Après avoir fait votre compilation croisée vers Win32 il faudra le tester avec [Wine](https://www.winehq.org/) ou (mieux) sur une machine Windows.
1616
1717### Pourquoi *nix vers Windows et pas l'inverse ?
1818
1919La 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.
2020
2121Vous auriez besoin d'utiliser les librairies des plateformes cibles (gtk, glib, libc, ...) et beaucoup de configuration pour *ld* (library paths, dynlinker path etc)
2222
2323This 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.
2424
2525### Télécharger les sources de FPC
2626
2727Les binaires ne sont pas suffisants. Vous aurez besoin de toutes les sources de FPC Voir [le site principale](http://www.freepascal.org). 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)*
2828
2929### Télécharger les binutils GNU
3030
3131Dans 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](http://ftp.gnu.org/gnu/binutils/)
3232
3333### les binutils de la compilation croisée
3434
3535Dans le dépôt de fpcbuild il existe un script pour les binutils pour toutes les plateformes: install/cross/buildcrossbinutils
3636
3737Télécharger et installer fpcbuild Via SVN:
3838
39-` []$ cd ~`\
40-` []$ svn co `[`http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install`](http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install)` install`
39+```text
40+ []$ cd ~
41+ []$ svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_2/install install
42+```
4143
4244Crée une copie du script
4345
44-` []$ cd ~/install/cross/`\
45-` []$ cp buildcrossbinutils buildcrossbinutils.sh`
46+```text
47+ []$ cd ~/install/cross/
48+ []$ cp buildcrossbinutils buildcrossbinutils.sh
49+```
4650
4751Éditer les variables :
4852
4953BASE pointe le dossier de construction et d'installation. Mettez de préférence un dossier vide Par exemple
5054
51-` BASE=~/cross_fpc`
55+```text
56+ BASE=~/cross_fpc
57+```
5258
5359Maintenant on va spécifier où sont les binutils (ici on suppose qu'ils sont dans l'archive ~/download/binutils-2.18.tar.gz)
5460
55-` BINUTILSPATH=~/download/`\
56-` BINUTILSBASE=binutils`\
57-` BINUTILSVERSION=2.18`\
58-` BINUTILS_GZIP=yes`
61+```text
62+ BINUTILSPATH=~/download/
63+ BINUTILSBASE=binutils
64+ BINUTILSVERSION=2.18
65+ BINUTILS_GZIP=yes
66+```
5967
6068Le 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
6169
62-` TARGETS_WIN="mingw32"`\
63-` `
70+```text
71+ TARGETS_WIN="mingw32"
72+```
6473
6574Commentez alors les autres cibles
6675
67-` #BSD="freebsd netbsd openbsd"`\
68-` #TARGETS_WIN="cygwin mingw32 msdosdjgpp"`\
69-` #TARGETS_I386="${BSD} linux solaris darwin"`\
70-` #TARGETS_POWERPC="${BSD} linux darwin"`\
71-` #TARGETS_SPARC="${BSD} linux solaris"`\
72-` #TARGETS_M68k=`
76+```text
77+ #BSD="freebsd netbsd openbsd"
78+ #TARGETS_WIN="cygwin mingw32 msdosdjgpp"
79+ #TARGETS_I386="${BSD} linux solaris darwin"
80+ #TARGETS_POWERPC="${BSD} linux darwin"
81+ #TARGETS_SPARC="${BSD} linux solaris"
82+ #TARGETS_M68k=
83+```
7384
7485Alors lancer le script
7586
76-` []$ sh buildcrossbinutils.sh`\
77-` `
87+```text
88+ []$ sh buildcrossbinutils.sh
89+```
7890
7991Le script va crée un sous dossier *logs* qui contient pleins de logs. Utiliser le en cas de problème
8092
8193Notez que pour plusieurs plateformes (Linux,FreeBSD, win32) these are available in compiled form already. Voir <ftp://freepascal.stack.nl/pub/fpc/contrib/cross/>
8294
8395### Compilation croisée de FPC
8496
8597Dans le répertoire de fpcbuild il existe un script pour construire une snapshot de FPC pour toutes les compilations croisées: install/cross/buildcrosssnapshot Crée une copie de secours du script
8698
87-` []$ cd ~/install/cross/`\
88-` []$ cp buildcrosssnapshot buildcrosssnapshot.sh`
99+```text
100+ []$ cd ~/install/cross/
101+ []$ cp buildcrosssnapshot buildcrosssnapshot.sh
102+```
89103
90104Éditer les variables au début du script. Normalement vous n'aurez à changez que *CROSSTOOLSROOT*, *FPCCVS*, *DESTDIR*, *TARGETS_OS* et *TARGETS_CPU*. Par exemple :
91105
92-` CROSSTOOLSROOT=~/cross_fpc/cross`\
93-` FPCCVS=~/sources/fpc`\
94-` TARGETS_OS="win32"`\
95-` TARGETS_CPU="i386"`\
96-` DESTDIR=~/cross_fpc/`\
97-` `
106+```text
107+ CROSSTOOLSROOT=~/cross_fpc/cross
108+ FPCCVS=~/sources/fpc
109+ TARGETS_OS="win32"
110+ TARGETS_CPU="i386"
111+ DESTDIR=~/cross_fpc/
112+```
98113
99114Puis lancer le script
100115
101-` []$ sh buildcrosssnapshot.sh`
116+```text
117+ []$ sh buildcrosssnapshot.sh
118+```
102119
103120Après cela vous aurez les binaires nécessaire à la compilation croisée pour Win32 sous i386 dans ~/cross_fpc
104121
105122### Configurez votre fpc.cfg
106123
107124Éditer en root votre /etc/fpc.cfg ou copier le dans ~/fpc.cfg et éditer ce fichier (**crée une sauvegarde avant de l'éditer !**) .Chercher dans la configuration les chemins de recherches des unités
108125
109126Note: Sur les anciens FPC \$version et \$target sont nommés \$fpcversion et \$fpctarget
110127
111-`-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget`\
112-`-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*`\
113-`-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl`
128+```text
129+-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
130+-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
131+-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
132+```
114133
115134Remplacer les par bos chemins vers les autres plateformes. Par exemple pour compiler sous win32 en i386:
116135
117-`#IFDEF win32`\
118-`-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/`\
119-`-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/*`\
120-`-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/rtl`\
121-`-XPi686-mingw32-`\
122-`-FD~/cross_fpc/cross/bin`\
123-`#ELSE linux`\
124-`-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget`\
125-`-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*`\
126-`-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl`\
127-`#-Fu~/fpc/packages/*;~/fpc/rtl/linux`\
128-`#ENDIF`
136+```text
137+#IFDEF win32
138+-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/
139+-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/*
140+-Fu~/cross_fpc/lib/fpc/$fpcversion/cross/i386-win32/units/rtl
141+-XPi686-mingw32-
142+-FD~/cross_fpc/cross/bin
143+#ELSE linux
144+-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
145+-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
146+-Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
147+#-Fu~/fpc/packages/*;~/fpc/rtl/linux
148+#ENDIF
149+```
129150
130151ToDo: Test and troubleshooting
131152
132153### Faire la compilation croisée pour la LCL et les composants Lazarus
133154
134155En ligne de commande
135156
136-`cd lazarus; make clean all OS_TARGET=win32`\
137-`cd lazarus/lcl; make clean all `
157+```text
158+cd lazarus; make clean all OS_TARGET=win32
159+cd lazarus/lcl; make clean all
160+```
138161
139162Cela va tout d'abord tout compiler pour Win32 (dont l'IDE inutile en Win32).
140163
141164Ou sinon dans l'IDE: Set LCL, Synedit, Codetools, Package Registration and IDE Interface to Clean+Build, set LCL interface to win32/win64 et mettez 'Target OS' à win32. Enfin construisez Lazarus. C'est 4 parties have splitted output directories, mais vos fichiers .ppu/.o pour Linux n'ont *pas* été écraser et vous n'avez pas à les recompiler.
142165
143166### Compiler un projet en compilation croisée
144167
145168Mettez dabs Project-\>Compiler Options-\>Code Target OS a 'win32' et le chemin de 'LCL Widget Type' à win32. C'est tout ! Maintenant quand vous allez construire vous allez crée un éxécutable Win32. L'IDE va également rescannez les unités (ici Win32), donc la complétition de code va alors être réglez pour les unités Win32. Quand vous allez charger un autre projet l'IDE va automatiquement reprendre la configuration du projet (s'il ne le fait pas c'est pas grave).
146169
147170### Conseil pour la compilation croisée et Lazarus
148171
149172If 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.
150173
151174Example 1: Cross compiling a project for linux and win32.
152175
153176Set 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).
154177
155178Example 2: Cross compiling a project for various platforms and widget sets.
156179
157180Set 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.
158181
159182The same can be done for packages.
160183
161184### Cross compiling and Lazarus Packages
162185
163186Lazarus 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.
164187
165188For example:
166189
167190Package A has as output directory: lib/\$(TargetOS) Project uses A.
168191
1691921. The project is built for linux. The IDE compiles A for linux in \<PackageDirOfA\>/lib/linux/, then it compiles the project for linux.
1701932. The project is built for win32. The IDE compiles A for win32 in \<PackageDirOfA\>/lib/win32/, then it compiles the project for win32.
1711943. 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.
172195
173196So, using the macros saves a lot of time.
174197
175198## For Unix (general)
176199
177200Option -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\>.
178201
179202Option -XR\<sysroot\> (recent trunk) that can be used to specify the target system root. It's used for:
180203
181204- 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.
182205- detecting the C library (linux specific): glibc or uclibc. E.g. for uclibc detection '\<sysroot\>/lib/ld-uClibc.so.0' is tried.
183206
184207## For Linux under Win32
185208
186209This is less trivial, there is some info in the [[buildfaq]]
187210
188211## FreeBSD to sparc
189212
190213I managed to crosscompile from x86 to Sparc Solaris 9. However the result doesn't work very well, but here is my cmdline:
191214
192215in compiler/ execute:
193216
194-` gmake cycle CPU_TARGET=sparc OS_TARGET=solaris CROSSBINUTILPREFIX=solaris-sparc- CROSSOPT='-Xd -Fl~/src/sollib'`
217+```text
218+ gmake cycle CPU_TARGET=sparc OS_TARGET=solaris CROSSBINUTILPREFIX=solaris-sparc- CROSSOPT='-Xd -Fl~/src/sollib'
219+```
195220
196221~/src/sollib is a directory that contains:
197222
198223- a set of .o's from /usr/local/gcc-3.3-32bit/lib/gcc-lib/sparc-sun-solaris/3.3
199224- libgcc.a from /usr/local/gcc-3.3-32bit/lib/gcc-lib/sparc-sun-solaris/3.3
200225- a set of lib*.so from /usr/lib: libaio.so libmd5.so libc.so libelf.so librt.so libdl.so libm.so
201226
202227Problem is illustrated by the following binary.
203228
204-` Free Pascal Compiler version 2.1.1 [2006/03/17] for sparc`\
205-` Copyright (c) 1993-2005 by Florian Klaempfl`\
206-` Target OS: Solaris for SPARC`\
207-` Compiling system.pp`\
208-` system.pp(15,1) Fatal: Syntax error, "BEGIN" expected but "identifier UNIT" found`
229+``` pascal
230+ Free Pascal Compiler version 2.1.1 [2006/03/17] for sparc
231+ Copyright (c) 1993-2005 by Florian Klaempfl
232+ Target OS: Solaris for SPARC
233+ Compiling system.pp
234+ system.pp(15,1) Fatal: Syntax error, "BEGIN" expected but "identifier UNIT" found
235+```
209236
210237I suspect wrong .o's are taken.
211238
212239## For Mac OS X
213240
214241See this [forum post](http://www.lazarus.freepascal.org/index.php?name=PNphpBB2&file=viewtopic&p=7222#7222). **Le lien est brisée.**
215242
216243## Original Contributors
217244
218245This page has been converted from the epikwiki [version](http://lazarus-ccr.sourceforge.net/index.php?wiki=CrossCompilingForWin32UnderLinux).
219246
220247---
221248*From the Free Pascal wiki, page "Cross compiling for Win32 under Linux/fr", as it stood on 2021-07-20 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
222249Category: Français

Back to history