2
$PostgreSQL: pgsql/doc/src/sgml/dfunc.sgml,v 1.28.4.1 2005-01-22 23:05:47 momjian Exp $
6
<title id="dfunc-title">Compiling and Linking Dynamically-Loaded Functions</title>
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
12
file that can be dynamically loaded by the server. To be precise, a
13
<firstterm>shared library</firstterm> needs to be
14
created.<indexterm><primary>shared library</></indexterm>
19
For information beyond what is contained in this section
20
you should read the documentation of your
21
operating system, in particular the manual pages for the C compiler,
22
<command>cc</command>, and the link editor, <command>ld</command>.
23
In addition, the <productname>PostgreSQL</productname> source code
24
contains several working examples in the
25
<filename>contrib</filename> directory. If you rely on these
26
examples you will make your modules dependent on the availability
27
of the <productname>PostgreSQL</productname> source code, however.
31
<indexterm><primary>PIC</></> Creating shared libraries is generally
32
analogous to linking executables: first the source files are
33
compiled into object files, then the object files are linked
34
together. The object files need to be created as
35
<firstterm>position-independent code</firstterm>
36
(<acronym>PIC</acronym>),<indexterm><primary>PIC</></> which
37
conceptually means that they can be placed at an arbitrary location
38
in memory when they are loaded by the executable. (Object files
39
intended for executables are usually not compiled that way.) The
40
command to link a shared library contains special flags to
41
distinguish it from linking an executable (at least in theory
42
— on some systems the practice is much uglier).
46
In the following examples we assume that your source code is in a
47
file <filename>foo.c</filename> and we will create a shared library
48
<filename>foo.so</filename>. The intermediate object file will be
49
called <filename>foo.o</filename> unless otherwise noted. A shared
50
library can contain more than one object file, but we only use one
55
Note: Reading GNU Libtool sources is generally a good way of
56
figuring out this information. The methods used within PostgreSQL
57
source code are not necessarily ideal.
62
<term><systemitem class="osname">BSD/OS</></term>
63
<indexterm><primary>BSD/OS</><secondary>shared library</></>
66
The compiler flag to create <acronym>PIC</acronym> is
67
<option>-fpic</option>. The linker flag to create shared
68
libraries is <option>-shared</option>.
71
ld -shared -o foo.so foo.o
73
This is applicable as of version 4.0 of
74
<systemitem class="osname">BSD/OS</>.
80
<term><systemitem class="osname">FreeBSD</></term>
81
<indexterm><primary>FreeBSD</><secondary>shared library</></>
84
The compiler flag to create <acronym>PIC</acronym> is
85
<option>-fpic</option>. To create shared libraries the compiler
86
flag is <option>-shared</option>.
89
gcc -shared -o foo.so foo.o
91
This is applicable as of version 3.0 of
92
<systemitem class="osname">FreeBSD</>.
98
<term><systemitem class="osname">HP-UX</></term>
99
<indexterm><primary>HP-UX</><secondary>shared library</></>
102
The compiler flag of the system compiler to create
103
<acronym>PIC</acronym> is <option>+z</option>. When using
104
<application>GCC</application> it's <option>-fpic</option>. The
105
linker flag for shared libraries is <option>-b</option>. So
115
ld -b -o foo.sl foo.o
117
<systemitem class="osname">HP-UX</> uses the extension
118
<filename>.sl</filename> for shared libraries, unlike most other
125
<term><systemitem class="osname">IRIX</></term>
126
<indexterm><primary>IRIX</><secondary>shared library</></>
129
<acronym>PIC</acronym> is the default, no special compiler
130
options are necessary. The linker option to produce shared
131
libraries is <option>-shared</option>.
134
ld -shared -o foo.so foo.o
141
<term><systemitem class="osname">Linux</></term>
142
<indexterm><primary>Linux</><secondary>shared library</></>
145
The compiler flag to create <acronym>PIC</acronym> is
146
<option>-fpic</option>. On some platforms in some situations
147
<option>-fPIC</option> must be used if <option>-fpic</option>
148
does not work. Refer to the GCC manual for more information.
149
The compiler flag to create a shared library is
150
<option>-shared</option>. A complete example looks like this:
153
cc -shared -o foo.so foo.o
160
<term><systemitem class="osname">MacOS X</></term>
161
<indexterm><primary>MacOS X</><secondary>shared library</></>
164
Here is an example. It assumes the developer tools are installed.
167
cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o
174
<term><systemitem class="osname">NetBSD</></term>
175
<indexterm><primary>NetBSD</><secondary>shared library</></>
178
The compiler flag to create <acronym>PIC</acronym> is
179
<option>-fpic</option>. For <acronym>ELF</acronym> systems, the
180
compiler with the flag <option>-shared</option> is used to link
181
shared libraries. On the older non-ELF systems, <literal>ld
182
-Bshareable</literal> is used.
185
gcc -shared -o foo.so foo.o
192
<term><systemitem class="osname">OpenBSD</></term>
193
<indexterm><primary>OpenBSD</><secondary>shared library</></>
196
The compiler flag to create <acronym>PIC</acronym> is
197
<option>-fpic</option>. <literal>ld -Bshareable</literal> is
198
used to link shared libraries.
201
ld -Bshareable -o foo.so foo.o
208
<term><systemitem class="osname">Solaris</></term>
209
<indexterm><primary>Solaris</><secondary>shared library</></>
212
The compiler flag to create <acronym>PIC</acronym> is
213
<option>-KPIC</option> with the Sun compiler and
214
<option>-fpic</option> with <application>GCC</>. To
215
link shared libraries, the compiler option is
216
<option>-G</option> with either compiler or alternatively
217
<option>-shared</option> with <application>GCC</>.
220
cc -G -o foo.so foo.o
225
gcc -G -o foo.so foo.o
232
<term><systemitem class="osname">Tru64 UNIX</></term>
233
<indexterm><primary>Tru64 UNIX</><secondary>shared library</></>
234
<indexterm><primary>Digital UNIX</><see>Tru64 UNIX</></>
237
<acronym>PIC</acronym> is the default, so the compilation command
238
is the usual one. <command>ld</command> with special options is
239
used to do the linking:
242
ld -shared -expect_unresolved '*' -o foo.so foo.o
244
The same procedure is used with GCC instead of the system
245
compiler; no special options are required.
251
<term><systemitem class="osname">UnixWare</></term>
252
<indexterm><primary>UnixWare</><secondary>shared library</></>
255
The compiler flag to create <acronym>PIC</acronym> is <option>-K
256
PIC</option> with the SCO compiler and <option>-fpic</option>
257
with <productname>GCC</productname>. To link shared libraries,
258
the compiler option is <option>-G</option> with the SCO compiler
259
and <option>-shared</option> with
260
<productname>GCC</productname>.
263
cc -G -o foo.so foo.o
268
gcc -shared -o foo.so foo.o
278
If this is too complicated for you, you should consider using
279
<ulink url="http://www.gnu.org/software/libtool/"><productname>GNU
280
Libtool</productname></ulink>, which hides the platform differences
281
behind a uniform interface.
286
The resulting shared library file can then be loaded into
287
<productname>PostgreSQL</productname>. When specifying the file name
288
to the <command>CREATE FUNCTION</command> command, one must give it
289
the name of the shared library file, not the intermediate object file.
290
Note that the system's standard shared-library extension (usually
291
<literal>.so</literal> or <literal>.sl</literal>) can be omitted from
292
the <command>CREATE FUNCTION</command> command, and normally should
293
be omitted for best portability.
297
Refer back to <xref linkend="xfunc-c-dynload"> about where the
298
server expects to find the shared library files.
302
Under AIX, object files are compiled normally but building the shared
303
library requires a couple of steps. First, create the object file:
305
cc <other flags> -c foo.c
307
You must then create a symbol \*(lqexports\*(rq file for the object
310
mkldexport foo.o `pwd` > foo.exp
312
Finally, you can create the shared library:
314
ld <other flags> -H512 -T512 -o foo.so -e _nostart \e
315
-bI:.../lib/postgres.exp -bE:foo.exp foo.o \e
322
<!-- Keep this comment at the end of the file
327
sgml-minimize-attributes:nil
328
sgml-always-quote-attributes:t
330
sgml-indent-tabs-mode:nil
332
sgml-parent-document:nil
333
sgml-default-dtd-file:"./reference.ced"
334
sgml-exposed-tags:nil
335
sgml-local-catalogs:("/usr/share/sgml/catalog")
336
sgml-local-ecat-files:nil