Using C++ with CREATE FUNCTION - Mailing list pgsql-general

From Mike Mascari
Subject Using C++ with CREATE FUNCTION
Date
Msg-id 3CFD8ABF.10E1392D@mascari.com
Whole thread Raw
List pgsql-general
I'm having a bit of trouble getting a shared module written in C++ to
load correctly with CREATE FUNCTION. The C source is:

#include "postgres.h"
#include <string.h>
#include "fmgr.h"

PG_FUNCTION_INFO_V1(echo);

Datum echo(PG_FUNCTION_ARGS) {

  text *t1 = (text *) PG_GETARG_TEXT_P(0);
  text *tr;
  tr = (text *) palloc(VARSIZE(t1));
  VARATT_SIZEP(tr) = VARSIZE(t1);
  memcpy(VARDATA(tr), VARDATA(t1), VARSIZE(t1) - VARHDRSZ);
  PG_RETURN_TEXT_P(tr);

}

I compile with:

cc -fPIC -c textutils.c -I/usr/include/pgsql/server
cc -shared -o pgblade.so textutils.o
cp pgblade.so /usr/local/mascari/lib

I create the function as:

CREATE FUNCTION echo(text) RETURNS text
AS '/usr/local/mascari/lib/pgblade.so'
LANGUAGE 'c' WITH (isStrict);

and

SELECT echo('Hello, World');

works as expected. However, if I:

1. Prevent name mangling by adding:

extern "C" {
  Datum echo(PG_FUNCTION_ARGS);
};

2. Rename textutils.c to textutils.cxx

3. Recompile with the same commands and drop/create the function as the
above, I get:

template1=# select echo('Hello, World');
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

Any tips?

This is:

Linux 2.2.19
PostgreSQL 7.2
egcs-2.91.66

Mike Mascari
mascarm@mascari.com

pgsql-general by date:

Previous
From: Hunter Hillegas
Date:
Subject: Re: Postgres 7.2.1 Really Spinning the CPU
Next
From: Neil Conway
Date:
Subject: Re: Postgres 7.2.1 Really Spinning the CPU