Using C++ with CREATE FUNCTION

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Using C++ with CREATE FUNCTION
Date: 2002-06-05 03:51:27
Message-ID: 3CFD8ABF.10E1392D@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)mascari(dot)com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Neil Conway 2002-06-05 06:09:11 Re: Postgres 7.2.1 Really Spinning the CPU
Previous Message Hunter Hillegas 2002-06-05 03:20:30 Re: Postgres 7.2.1 Really Spinning the CPU