forked from gleu/pgdocs_fr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfunc.xml
312 lines (289 loc) · 12 KB
/
dfunc.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?xml version="1.0" encoding="UTF-8"?>
<!-- Dernière modification
le $Date$
par $Author$
révision $Revision$ -->
<!-- SAS : 20070511, PG824 -->
<sect2 id="dfunc">
<title id="dfunc-title">Compiler et lier des fonctions chargées dynamiquement</title>
<para>
Avant de pouvoir être utilisées dans <productname>PostgreSQL</productname>,
les fonctions d'extension écrites en C doivent être compilées et liées
d'une certaine façon, ceci afin de produire un fichier
dynamiquement chargeable par le serveur. Pour être plus précis, une
<firstterm>bibliothèque partagée</firstterm> doit être créée.
<indexterm><primary>bibliothèque partagée</primary></indexterm>
</para>
<para>
Pour obtenir plus d'informations que celles contenues dans cette section,
il faut se référer à la documentation du système d'exploitation, en
particulier les pages traitant du compilateur C, de <command>cc</command> et
de l'éditeur de lien, <command>ld</command>. Par ailleurs, le code
source de <productname>PostgreSQL</productname> contient de nombreux exemples
fonctionnels dans le répertoire <filename>contrib</filename>.
Néanmoins, ces exemples entraînent la création de modules
qui dépendent de la disponibilité du code source de
<productname>PostgreSQL</productname>.
</para>
<para>
La création de bibliothèques partagées est un
processus analogue à celui utilisé pour lier des exécutables :
les fichiers sources sont d'abord compilés en fichiers objets puis sont liées ensemble.
Les fichiers objets doivent être compilés sous la forme de <firstterm>code
indépendant de sa position</firstterm> (<acronym>PIC</acronym>, acronyme de
<foreignphrase>position-independent code</foreignphrase>)
<indexterm><primary>PIC</primary></indexterm>.
Conceptuellement, cela signifie qu'ils peuvent être placés dans
une position arbitraire de la mémoire lorsqu'ils sont chargés par
l'exécutable. (Les fichiers objets destinés aux exécutables ne sont
généralement pas compilés de cette manière.) La commande qui permet de lier
des bibliothèques partagées nécessite des options spéciales qui la
distinguent de celle permettant de lier un exécutable. En théorie, tout du
moins. La réalité est, sur certains systèmes, beaucoup plus complexe.
</para>
<para>
Les exemples suivants considèrent que le code source est un
fichier <filename>foo.c</filename> et qu'une bibliothèque partagée
<filename>foo.so</filename> doit être créée. Sans précision, le fichier objet
intermédiaire est appelé <filename>foo.o</filename>. Une bibliothèque
partagée peut contenir plusieurs fichiers objet. Cela dit, un seul est
utilisé ici.
</para>
<!--
Note: Reading GNU Libtool sources is generally a good way of
figuring out this information. The methods used within PostgreSQL
source code are not necessarily ideal.
-->
<variablelist>
<varlistentry>
<term><systemitem class="osname">BSD/OS</systemitem></term>
<listitem>
<indexterm><primary>BSD/OS</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur pour créer des <acronym>PIC</acronym> est
<option>-fpic</option>. L'option de l'éditeur de liens pour créer des
bibliothèques partagées est <option>-shared</option>.
<programlisting>gcc -fpic -c foo.c
ld -shared -o foo.so foo.o
</programlisting>
Ceci est applicable à partir de la version 4.0 de
<systemitem class="osname">BSD/OS</systemitem>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">FreeBSD</systemitem></term>
<listitem>
<indexterm><primary>FreeBSD</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur pour créer des <acronym>PIC</acronym> est
<option>-fpic</option>. L'option de l'éditeur de liens pour créer des
bibliothèques partagées est <option>-shared</option>.
<programlisting>gcc -fpic -c foo.c
gcc -shared -o foo.so foo.o
</programlisting>
Ceci est applicable à partir de la version 3.0 de
<systemitem class="osname">FreeBSD</systemitem>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">HP-UX</systemitem></term>
<listitem>
<indexterm><primary>HP-UX</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur du système pour créer des <acronym>PIC</acronym>
est <option>+z</option>. Avec <application>GCC</application>, l'option
est <option>-fpic</option>. Le
commutateur de l'éditeur de liens pour les bibliothèques partagées est
<option>-b</option>.
Ainsi :
<programlisting>cc +z -c foo.c
</programlisting>
ou :
<programlisting>gcc -fpic -c foo.c
</programlisting>
puis :
<programlisting>ld -b -o foo.sl foo.o
</programlisting>
<systemitem class="osname">HP-UX</systemitem> utilise l'extension
<filename>.sl</filename> pour les bibliothèques partagées, à la différence
de la plupart des autres systèmes.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">IRIX</systemitem></term>
<listitem>
<indexterm><primary>IRIX</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
<acronym>PIC</acronym> est l'option par défaut. Aucune option de compilation
particulière n'est nécessaire. Le commutateur de l'éditeur de liens pour
produire des bibliothèques partagées est <option>-shared</option> :
<programlisting>cc -c foo.c
ld -shared -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">Linux</systemitem></term>
<listitem>
<indexterm><primary>Linux</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur pour créer des <acronym>PIC</acronym> est
<option>-fpic</option>. Sur certaines plateformes et dans certaines
situations, <option>-fPIC</option> doit être utilisé si
<option>-fpic</option> ne fonctionne pas. Le manuel de GCC
donne plus d'informations. L'option de compilation pour créer des
bibliothèques partagées est <option>-shared</option>. Un exemple complet
ressemble à :
<programlisting>cc -fpic -c foo.c
cc -shared -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">MacOS X</systemitem></term>
<listitem>
<indexterm><primary>MacOS X</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'exemple suivant suppose que les outils de développement sont installés.
<programlisting>cc -c foo.c
cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">NetBSD</systemitem></term>
<listitem>
<indexterm><primary>NetBSD</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur pour créer des <acronym>PIC</acronym> est
<option>-fpic</option>. Pour les systèmes <acronym>ELF</acronym>,
l'option de compilation pour lier les bibliothèques partagées
est <option>-shared</option>. Sur les systèmes plus anciens et non-ELF,
on utilise <literal>ld -Bshareable</literal>.
<programlisting>gcc -fpic -c foo.c
gcc -shared -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">OpenBSD</systemitem></term>
<listitem>
<indexterm><primary>OpenBSD</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur pour créer des <acronym>PIC</acronym> est
<option>-fpic</option>. Les bibliothèques partagées peuvent être créées avec
<literal>ld -Bshareable</literal>.
<programlisting>gcc -fpic -c foo.c
ld -Bshareable -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">Solaris</systemitem></term>
<listitem>
<indexterm><primary>Solaris</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option du compilateur pour créer des <acronym>PIC</acronym> est
<option>-KPIC</option> avec le compilateur de Sun et <option>-fpic</option>
avec <application>GCC</application>. Pour lier les bibliothèques
partagées, l'option de compilation est respectivement <option>-G</option>
ou <option>-shared</option>.
<programlisting>cc -KPIC -c foo.c
cc -G -o foo.so foo.o
</programlisting>
ou
<programlisting>gcc -fpic -c foo.c
gcc -G -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">Tru64 UNIX</systemitem></term>
<listitem>
<indexterm><primary>Tru64 UNIX</primary><secondary>bibliothèque partagée</secondary></indexterm>
<indexterm><primary>Digital UNIX</primary><see>Tru64 UNIX</see></indexterm>
<para>
Par défaut, il s'agit de <acronym>PIC</acronym>. Ainsi, aucune directive
particulière n'est à fournir pour la compilation. Pour l'édition de lien, des
options spécifiques sont à fournir à <command>ld</command> :
<programlisting>cc -c foo.c
ld -shared -expect_unresolved '*' -o foo.so foo.o
</programlisting>
Une procédure identique doit être employée dans le cas où GCC est
utilisé à la place du compilateur du système ; aucune option
particulière n'est nécessaire.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><systemitem class="osname">UnixWare</systemitem></term>
<listitem>
<indexterm><primary>UnixWare</primary><secondary>bibliothèque partagée</secondary></indexterm>
<para>
L'option de compilation pour créer des <acronym>PIC</acronym> est
<option>-KPIC</option> avec le compilateur SCO et <option>-fpic</option> avec
<productname>GCC</productname>. Pour lier des bibliothèques partagées,
les options respectives sont <option>-G</option> et
<option>-shared</option>.
<programlisting>cc -K PIC -c foo.c
cc -G -o foo.so foo.o
</programlisting>
ou
<programlisting>gcc -fpic -c foo.c
gcc -shared -o foo.so foo.o
</programlisting>
</para>
</listitem>
</varlistentry>
</variablelist>
<tip>
<para>
Si cela s'évère trop compliqué,
<ulink url="http://www.gnu.org/software/libtool/"><productname>GNU Libtool</productname></ulink>
peut être utilisé. Cet outil permet de s'affranchir des
différences entre les nombreux systèmes au travers d'une interface
uniformisée.
</para>
</tip>
<para>
La bibliothèque partagée résultante peut être chargée dans
<productname>PostgreSQL</productname>. Lorsque l'on précise le nom du fichier
dans la commande <command>CREATE FUNCTION</command>, il faut indiquer le nom
de la bibliothèque partagée et non celui du fichier objet intermédiaire.
L'extension standard pour les bibliothèques partagées (en général
<literal>.so</literal> ou <literal>.sl</literal>) peut être omise dans la commande
<command>CREATE FUNCTION</command>, et doit l'être pour une meilleure
portabilité.
</para>
<para>
La <xref linkend="xfunc-c-dynload"/> indique l'endroit où
le serveur s'attend à trouver les fichiers de bibliothèques partagées.
</para>
<!--
Under AIX, object files are compiled normally but building the shared
library requires a couple of steps. First, create the object file:
.nf
cc <other flags> -c foo.c
.fi
You must then create a symbol \*(lqexports\*(rq file for the object
file:
.nf
mkldexport foo.o `pwd` > foo.exp
.fi
Finally, you can create the shared library:
.nf
ld <other flags> -H512 -T512 -o foo.so -e _nostart \e
-bI:.../lib/postgres.exp -bE:foo.exp foo.o \e
-lm -lc 2>/dev/null
.fi
-->
</sect2>