]> granicus.if.org Git - postgresql/blob - doc/src/sgml/dfunc.sgml
Make spelling of operating system names match official version closely.
[postgresql] / doc / src / sgml / dfunc.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/dfunc.sgml,v 1.19 2002/01/09 00:52:37 petere Exp $
3 -->
4
5 <sect2 id="dfunc">
6  <title id="dfunc-title">Compiling and Linking Dynamically-Loaded Functions</title>
7
8  <para>
9   Before you are able to use your
10   <productname>PostgreSQL</productname> extension functions written in
11   C, they must be compiled and linked in a special way to produce a file
12   that can be dynamically loaded by the server.  To be
13   precise, a <firstterm>shared library</firstterm> needs to be created.
14  </para>
15
16  <para>
17   For more information you should read the documentation of your
18   operating system, in particular the manual pages for the C compiler,
19   <command>cc</command>, and the link editor, <command>ld</command>.
20   In addition, the <productname>PostgreSQL</productname> source code
21   contains several working examples in the
22   <filename>contrib</filename> directory.  If you rely on these
23   examples you will make your modules dependent on the availability
24   of the <productname>PostgreSQL</productname> source code, however.
25  </para>
26
27  <para>
28   <indexterm><primary>PIC</></>
29   Creating shared libraries is generally analoguous to linking
30   executables:  first the source files are compiled into object files,
31   then the object files are linked together.  The object files need to
32   be created as <firstterm>position-independent code</firstterm>
33   (<acronym>PIC</acronym>), which conceptually means that they can be
34   placed at an arbitrary location in memory when they are loaded by the
35   executable.  (Object files intended for executables are usually not compiled
36   that way.)  The command to link a shared library contains special
37   flags to distinguish it from linking an executable. --- At least
38   this is the theory.  On some systems the practice is much uglier.
39  </para>
40
41  <para>
42   In the following examples we assume that your source code is in a
43   file <filename>foo.c</filename> and we will create a shared library
44   <filename>foo.so</filename>.  The intermediate object file will be
45   called <filename>foo.o</filename> unless otherwise noted.  A shared
46   library can contain more than one object file, but we only use one
47   here.
48  </para>
49
50  <para>
51
52 <!--
53   Note:  Reading GNU Libtool sources is generally a good way of figuring out
54   this information.  The methods used within
55  <productname>PostgreSQL</> source code are not
56   necessarily ideal.
57 -->
58
59   <variablelist>
60    <varlistentry>
61     <term><productname>BSD/OS</productname></term>
62     <indexterm><primary>BSD/OS</></>
63     <listitem>
64      <para>
65       The compiler flag to create <acronym>PIC</acronym> is
66       <option>-fpic</option>.  The linker flag to create shared
67       libraries is <option>-shared</option>.
68 <programlisting>
69 gcc -fpic -c foo.c
70 ld -shared -o foo.so foo.o
71 </programlisting>
72       This is applicable as of version 4.0 of
73       <productname>BSD/OS</productname>.
74      </para>
75     </listitem>
76    </varlistentry>
77
78    <varlistentry>
79     <term><productname>FreeBSD</productname></term>
80     <indexterm><primary>FreeBSD</></>
81     <listitem>
82      <para>
83       The compiler flag to create <acronym>PIC</acronym> is
84       <option>-fpic</option>.  To create shared libraries the compiler
85       flag is <option>-shared</option>.
86 <programlisting>
87 gcc -fpic -c foo.c
88 gcc -shared -o foo.so foo.o
89 </programlisting>
90       This is applicable as of version 3.0 of
91       <productname>FreeBSD</productname>.
92      </para>
93     </listitem>
94    </varlistentry>
95
96    <varlistentry>
97     <term><productname>HP-UX</productname></term>
98     <indexterm><primary>HP-UX</></>
99     <listitem>
100      <para>
101       The compiler flag of the system compiler to create
102       <acronym>PIC</acronym> is <option>+z</option>.  When using
103       <productname>GCC</productname> it's <option>-fpic</option>. The
104       linker flag for shared libraries is <option>-b</option>.  So
105 <programlisting>
106 cc +z -c foo.c
107 </programlisting>
108       or
109 <programlisting>
110 gcc -fpic -c foo.c
111 </programlisting>
112       and then
113 <programlisting>
114 ld -b -o foo.sl foo.o
115 </programlisting>
116       <productname>HP-UX</productname> uses the extension
117       <filename>.sl</filename> for shared libraries, unlike most other
118       systems.
119      </para>
120     </listitem>
121    </varlistentry>
122
123    <varlistentry>
124     <term><productname>IRIX</productname></term>
125     <indexterm><primary>IRIX</></>
126     <listitem>
127      <para>
128       <acronym>PIC</acronym> is the default, no special compiler
129       options are necessary.  The linker option to produce shared
130       libraries is <option>-shared</option>.
131 <programlisting>
132 cc -c foo.c
133 ld -shared -o foo.so foo.o
134 </programlisting>
135      </para>
136     </listitem>
137    </varlistentry>
138
139    <varlistentry>
140     <term><productname>Linux</productname></term>
141     <indexterm><primary>Linux</></>
142     <listitem>
143      <para>
144       The compiler flag to create <acronym>PIC</acronym> is
145       <option>-fpic</option>.  On some platforms in some situations
146       <option>-fPIC</option> must be used if <option>-fpic</option>
147       does not work.  Refer to the GCC manual for more information.
148       The compiler flag to create a shared library is
149       <option>-shared</option>.  A complete example looks like this:
150 <programlisting>
151 cc -fpic -c foo.c
152 cc -shared -o foo.so foo.o
153 </programlisting>
154      </para>
155     </listitem>
156    </varlistentry>
157
158    <varlistentry>
159     <term><productname>NetBSD</productname></term>
160     <indexterm><primary>NetBSD</></>
161     <listitem>
162      <para>
163       The compiler flag to create <acronym>PIC</acronym> is
164       <option>-fpic</option>.  For <acronym>ELF</acronym> systems, the
165       compiler with the flag <option>-shared</option> is used to link
166       shared libraries.  On the older non-ELF systems, <literal>ld
167       -Bshareable</literal> is used.
168 <programlisting>
169 gcc -fpic -c foo.c
170 gcc -shared -o foo.so foo.o
171 </programlisting>
172      </para>
173     </listitem>
174    </varlistentry>
175
176    <varlistentry>
177     <term><productname>OpenBSD</productname></term>
178     <indexterm><primary>OpenBSD</></>
179     <listitem>
180      <para>
181       The compiler flag to create <acronym>PIC</acronym> is
182       <option>-fpic</option>.  <literal>ld -Bshareable</literal> is
183       used to link shared libraries.
184 <programlisting>
185 gcc -fpic -c foo.c
186 ld -Bshareable -o foo.so foo.o
187 </programlisting>
188      </para>
189     </listitem>
190    </varlistentry>
191
192    <varlistentry>
193     <term><productname>Solaris</productname></term>
194     <indexterm><primary>Solaris</></>
195     <listitem>
196      <para>
197       The compiler flag to create <acronym>PIC</acronym> is
198       <option>-KPIC</option> with the Sun compiler and
199       <option>-fpic</option> with <productname>GCC</productname>.  To
200       link shared libraries, the compiler option is
201       <option>-G</option> with either compiler or alternatively
202       <option>-shared</option> with <productname>GCC</productname>.
203 <programlisting>
204 cc -KPIC -c foo.c
205 cc -G -o foo.so foo.o
206 </programlisting>
207       or
208 <programlisting>
209 gcc -fpic -c foo.c
210 gcc -G -o foo.so foo.o
211 </programlisting>
212      </para>
213     </listitem>
214    </varlistentry>
215
216    <varlistentry>
217     <term>Tru64 UNIX</term>   
218     <indexterm><primary>Tru64 UNIX</></>
219     <indexterm><primary>Digital UNIX</><see>Tru64 UNIX</></>
220     <listitem>
221      <para>
222       <acronym>PIC</acronym> is the default, so the compilation command
223       is the usual one.  <command>ld</command> with special options is
224       used to do the linking:
225 <programlisting>
226 cc -c foo.c
227 ld -shared -expect_unresolved '*' -o foo.so foo.o
228 </programlisting>
229       The same procedure is used with GCC instead of the system
230       compiler; no special options are required.
231      </para>
232     </listitem>
233    </varlistentry>
234
235    <varlistentry>
236     <term><productname>UnixWare</productname></term>
237     <indexterm><primary>UnixWare</></>
238     <listitem>
239      <para>
240       The compiler flag to create <acronym>PIC</acronym> is <option>-K
241       PIC</option> with the SCO compiler and <option>-fpic</option>
242       with <productname>GCC</productname>.  To link shared libraries,
243       the compiler option is <option>-G</option> with the SCO compiler
244       and <option>-shared</option> with
245       <productname>GCC</productname>.
246 <programlisting>
247 cc -K PIC -c foo.c
248 cc -G -o foo.so foo.o
249 </programlisting>
250       or
251 <programlisting>
252 gcc -fpic -c foo.c
253 gcc -shared -o foo.so foo.o
254 </programlisting>
255      </para>
256     </listitem>
257    </varlistentry>
258
259   </variablelist>
260  </para>
261
262  <tip>
263   <para>
264    If you want to package your extension modules for wide distribution
265    you should consider using <ulink
266    url="http://www.gnu.org/software/libtool/"><productname>GNU
267    Libtool</productname></ulink> for building shared libraries.  It
268    encapsulates the platform differences into a general and powerful
269    interface.  Serious packaging also requires considerations about
270    library versioning, symbol resolution methods, and other issues.
271   </para>
272  </tip>
273
274  <para>
275   The resulting shared library file can then be loaded into
276   <productname>PostgreSQL</productname>.  When specifying the file name
277   to the <command>CREATE FUNCTION</command> command, one must give it
278   the name of the shared library file, not the intermediate object file.
279   Note that the system's standard shared-library extension (usually
280   <literal>.so</literal> or <literal>.sl</literal>) can be omitted from
281   the <command>CREATE FUNCTION</command> command, and normally should
282   be omitted for best portability.
283  </para>
284
285  <para>
286   Refer back to <xref linkend="xfunc-c-dynload"> about where the
287   server expects to find the shared library files.
288  </para>
289
290 <!--
291 Under AIX, object files are compiled normally but building the shared
292 library requires a couple of steps.  First, create the object file:
293 .nf
294 cc <other flags> -c foo.c
295 .fi
296 You must then create a symbol \*(lqexports\*(rq file for the object
297 file:
298 .nf
299 mkldexport foo.o `pwd` > foo.exp
300 .fi
301 Finally, you can create the shared library:
302 .nf
303 ld <other flags> -H512 -T512 -o foo.so -e _nostart \e
304    -bI:.../lib/postgres.exp -bE:foo.exp foo.o \e
305    -lm -lc 2>/dev/null
306 .fi
307 You should look at the <citetitle>PostgreSQL User's Manual</>
308 for an explanation of this
309 procedure.
310
311   -->
312
313 </sect2>
314
315 <!-- Keep this comment at the end of the file
316 Local variables:
317 mode:sgml
318 sgml-omittag:nil
319 sgml-shorttag:t
320 sgml-minimize-attributes:nil
321 sgml-always-quote-attributes:t
322 sgml-indent-step:1
323 sgml-indent-tabs-mode:nil
324 sgml-indent-data:t
325 sgml-parent-document:nil
326 sgml-default-dtd-file:"./reference.ced"
327 sgml-exposed-tags:nil
328 sgml-local-catalogs:("/usr/share/sgml/catalog")
329 sgml-local-ecat-files:nil
330 End:
331 -->