Home Explore Blog CI



postgresql

3rd chunk of `doc/src/sgml/dfunc.sgml`
898bb94009c66ba7b536fcd4001a92223e115287ff89d1190000000100000cd3
 <indexterm><primary>macOS</primary><secondary>shared library</secondary></indexterm>
    </term>
    <listitem>
     <para>
      Here is an example.  It assumes the developer tools are installed.
<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>
     <indexterm><primary>NetBSD</primary><secondary>shared library</secondary></indexterm>
    </term>
    <listitem>
     <para>
      The compiler flag to create <acronym>PIC</acronym> is
      <option>-fPIC</option>.  For <acronym>ELF</acronym> systems, the
      compiler with the flag <option>-shared</option> is used to link
      shared libraries.  On the older non-ELF systems, <literal>ld
      -Bshareable</literal> is used.
<programlisting>
gcc -fPIC -c foo.c
gcc -shared -o foo.so foo.o
</programlisting>
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term>
     <systemitem class="osname">OpenBSD</systemitem>
     <indexterm><primary>OpenBSD</primary><secondary>shared library</secondary></indexterm>
    </term>
    <listitem>
     <para>
      The compiler flag to create <acronym>PIC</acronym> is
      <option>-fPIC</option>.  <literal>ld -Bshareable</literal> is
      used to link shared libraries.
<programlisting>
gcc -fPIC -c foo.c
ld -Bshareable -o foo.so foo.o
</programlisting>
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term>
     <systemitem class="osname">Solaris</systemitem>
     <indexterm><primary>Solaris</primary><secondary>shared library</secondary></indexterm>
    </term>
    <listitem>
     <para>
      The compiler flag to create <acronym>PIC</acronym> is
      <option>-KPIC</option> with the Sun compiler and
      <option>-fPIC</option> with <application>GCC</application>.  To
      link shared libraries, the compiler option is
      <option>-G</option> with either compiler or alternatively
      <option>-shared</option> with <application>GCC</application>.
<programlisting>
cc -KPIC -c foo.c
cc -G -o foo.so foo.o
</programlisting>
      or
<programlisting>
gcc -fPIC -c foo.c
gcc -G -o foo.so foo.o
</programlisting>
     </para>
    </listitem>
   </varlistentry>

  </variablelist>

 <tip>
  <para>
   If this is too complicated for you, you should consider using
   <ulink url="https://www.gnu.org/software/libtool/">
   <productname>GNU Libtool</productname></ulink>,
   which hides the platform differences behind a uniform interface.
  </para>
 </tip>

 <para>
  The resulting shared library file can then be loaded into
  <productname>PostgreSQL</productname>.  When specifying the file name
  to the <command>CREATE FUNCTION</command> command, one must give it
  the name of the shared library file, not the intermediate object file.
  Note that the system's standard shared-library extension (usually
  <literal>.so</literal> or <literal>.sl</literal>) can be omitted from
  the <command>CREATE FUNCTION</command> command, and normally should
  be omitted for best portability.
 </para>

 <para>
  Refer back to <xref linkend="xfunc-c-dynload"/> about where the
  server expects to find the shared library files.
 </para>

</sect2>

Title: Creating Shared Libraries for PostgreSQL
Summary
This section provides platform-specific examples for creating shared libraries using various compilers and flags, including macOS, NetBSD, OpenBSD, and Solaris, and recommends using GNU Libtool for simplifying the process, before loading the libraries into PostgreSQL.