Re: server-side extension in c++

Поиск
Список
Период
Сортировка
От Bruce Momjian
Тема Re: server-side extension in c++
Дата
Msg-id 201006010232.o512WbW13985@momjian.us
обсуждение исходный текст
Ответ на Re: server-side extension in c++  (Craig Ringer <craig@postnewspapers.com.au>)
Ответы Re: server-side extension in c++  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Craig Ringer wrote:
> Igor wrote:
> > Hi All,
> >
> > Is there an easy way to add c++ files to my simple pgsql module ? My Makefile
> > is as follows -
> >
> > ===
> > MODULES = pg_uservars
> > DATA_built = pg_uservars.sql
> > PGXS := $(shell pg_config --pgxs)
> > include $(PGXS)
> > ===
> >
> > I've got "pg_uservars.c" and "hv.cc" and I'd like to compile hv.cc via g++.
> > I'm aware of c++ name [de]mangling, just looking if there's a standard way of
> > using C++ when it comes to pgxs.
>
> It should "just work". Simply make sure to follow the usual rules for
> calling into C++ code from C and vice versa:
>
> - Use "extern C" linkage for all functions that must be accessible by
>   dlopen(), and preferably also for any functions that you might take
>   a function pointer to and pass to C code
>
> - Never return new()'d memory that might be free()'d by the C code; use
>   malloc()
>
> - Never delete() memory that was malloc()'d by the C code; use free()
>
> - Never let an exception propagate into the C code; use a catch-all
>   block at the top level of all "extern C" functions
>
> ... and probably other things I've missed.

That is great new information.  I have created a new documentation
section called "Using C++ for Extensibility", and listed you as the
author in the CVS commit;  patch attached.  Thanks.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + None of us is going to be here forever. +

Index: doc/src/sgml/extend.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/extend.sgml,v
retrieving revision 1.38
diff -c -c -r1.38 extend.sgml
*** doc/src/sgml/extend.sgml    3 Apr 2010 07:22:53 -0000    1.38
--- doc/src/sgml/extend.sgml    1 Jun 2010 02:29:31 -0000
***************
*** 273,276 ****
--- 273,322 ----
    &xoper;
    &xindex;

+   <sect1 id="extend-how">
+    <title>Using C++ for Extensibility</title>
+
+    <indexterm zone="extend-Cpp">
+     <primary>C++</primary>
+    </indexterm>
+
+    <para>
+     It is possible to use a compiler in C++ mode to build
+     <productname>PostgreSQL</productname> extensions;  you must simply
+     follow the standard methods for dynamically linking to C executables:
+
+     <itemizedlist>
+      <listitem>
+       <para>
+         Use <literal>extern C</> linkage for all functions that must
+         be accessible by <function>dlopen()</>.  This is also necessary
+         for any functions that might be passed as pointers between
+         the backend and C++ code.
+       </para>
+      </listitem>
+      <listitem>
+       <para>
+        Use <function>malloc()</> to allocate any memory that might be
+        freed by the backend C code (don't pass <function>new()</>-allocated
+        memory).
+       </para>
+      </listitem>
+      <listitem>
+       <para>
+        Use <function>free()</> to free memory allocated by the backend
+        C code (do not use <function>delete()</> for such cases).
+       </para>
+      </listitem>
+      <listitem>
+       <para>
+        Prevent exceptions from propagating into the C code (use a
+        catch-all block at the top level of all <literal>extern C</>
+        functions).
+       </para>
+      </listitem>
+     </itemizedlist>
+    </para>
+
+   </sect1>
+
   </chapter>

В списке pgsql-general по дате отправления:

Предыдущее
От: Nilesh Govindarajan
Дата:
Сообщение: Re: What Linux edition we should chose?
Следующее
От: Schwaighofer Clemens
Дата:
Сообщение: Re: What Linux edition we should chose?