2 $Header: /cvsroot/pgsql/doc/src/sgml/dfunc.sgml,v 1.21 2002/08/26 23:22: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 file
12 that can be dynamically loaded by the server. To be
13 precise, a <firstterm>shared library</firstterm> needs to be created.
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.
28 <indexterm><primary>PIC</></>
29 Creating shared libraries is generally analogous 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.
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
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
61 <term><productname>BSD/OS</productname></term>
62 <indexterm><primary>BSD/OS</></>
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>.
70 ld -shared -o foo.so foo.o
72 This is applicable as of version 4.0 of
73 <productname>BSD/OS</productname>.
79 <term><productname>FreeBSD</productname></term>
80 <indexterm><primary>FreeBSD</></>
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>.
88 gcc -shared -o foo.so foo.o
90 This is applicable as of version 3.0 of
91 <productname>FreeBSD</productname>.
97 <term><productname>HP-UX</productname></term>
98 <indexterm><primary>HP-UX</></>
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
114 ld -b -o foo.sl foo.o
116 <productname>HP-UX</productname> uses the extension
117 <filename>.sl</filename> for shared libraries, unlike most other
124 <term><productname>IRIX</productname></term>
125 <indexterm><primary>IRIX</></>
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>.
133 ld -shared -o foo.so foo.o
140 <term><productname>Linux</productname></term>
141 <indexterm><primary>Linux</></>
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:
152 cc -shared -o foo.so foo.o
159 <term><productname>NetBSD</productname></term>
160 <indexterm><primary>NetBSD</></>
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.
170 gcc -shared -o foo.so foo.o
177 <term><productname>OpenBSD</productname></term>
178 <indexterm><primary>OpenBSD</></>
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.
186 ld -Bshareable -o foo.so foo.o
193 <term><productname>OS X</productname></term>
194 <indexterm><primary>OS X</></>
197 Here is a sample. It assumes the developer tools are installed.
200 cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o
207 <term><productname>Solaris</productname></term>
208 <indexterm><primary>Solaris</></>
211 The compiler flag to create <acronym>PIC</acronym> is
212 <option>-KPIC</option> with the Sun compiler and
213 <option>-fpic</option> with <productname>GCC</productname>. To
214 link shared libraries, the compiler option is
215 <option>-G</option> with either compiler or alternatively
216 <option>-shared</option> with <productname>GCC</productname>.
219 cc -G -o foo.so foo.o
224 gcc -G -o foo.so foo.o
231 <term>Tru64 UNIX</term>
232 <indexterm><primary>Tru64 UNIX</></>
233 <indexterm><primary>Digital UNIX</><see>Tru64 UNIX</></>
236 <acronym>PIC</acronym> is the default, so the compilation command
237 is the usual one. <command>ld</command> with special options is
238 used to do the linking:
241 ld -shared -expect_unresolved '*' -o foo.so foo.o
243 The same procedure is used with GCC instead of the system
244 compiler; no special options are required.
250 <term><productname>UnixWare</productname></term>
251 <indexterm><primary>UnixWare</></>
254 The compiler flag to create <acronym>PIC</acronym> is <option>-K
255 PIC</option> with the SCO compiler and <option>-fpic</option>
256 with <productname>GCC</productname>. To link shared libraries,
257 the compiler option is <option>-G</option> with the SCO compiler
258 and <option>-shared</option> with
259 <productname>GCC</productname>.
262 cc -G -o foo.so foo.o
267 gcc -shared -o foo.so foo.o
278 If you want to package your extension modules for wide distribution
279 you should consider using <ulink
280 url="http://www.gnu.org/software/libtool/"><productname>GNU
281 Libtool</productname></ulink> for building shared libraries. It
282 encapsulates the platform differences into a general and powerful
283 interface. Serious packaging also requires considerations about
284 library versioning, symbol resolution methods, and other issues.
289 The resulting shared library file can then be loaded into
290 <productname>PostgreSQL</productname>. When specifying the file name
291 to the <command>CREATE FUNCTION</command> command, one must give it
292 the name of the shared library file, not the intermediate object file.
293 Note that the system's standard shared-library extension (usually
294 <literal>.so</literal> or <literal>.sl</literal>) can be omitted from
295 the <command>CREATE FUNCTION</command> command, and normally should
296 be omitted for best portability.
300 Refer back to <xref linkend="xfunc-c-dynload"> about where the
301 server expects to find the shared library files.
305 Under AIX, object files are compiled normally but building the shared
306 library requires a couple of steps. First, create the object file:
308 cc <other flags> -c foo.c
310 You must then create a symbol \*(lqexports\*(rq file for the object
313 mkldexport foo.o `pwd` > foo.exp
315 Finally, you can create the shared library:
317 ld <other flags> -H512 -T512 -o foo.so -e _nostart \e
318 -bI:.../lib/postgres.exp -bE:foo.exp foo.o \e
321 You should look at the <citetitle>PostgreSQL User's Manual</>
322 for an explanation of this
329 <!-- Keep this comment at the end of the file
334 sgml-minimize-attributes:nil
335 sgml-always-quote-attributes:t
337 sgml-indent-tabs-mode:nil
339 sgml-parent-document:nil
340 sgml-default-dtd-file:"./reference.ced"
341 sgml-exposed-tags:nil
342 sgml-local-catalogs:("/usr/share/sgml/catalog")
343 sgml-local-ecat-files:nil