Re: Calling C++ function

Lists: pgsql-interfaces
From: Oliver Archner <oliver(dot)archner(at)bitoek(dot)uni-bayreuth(dot)de>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Calling C++ function
Date: 2004-08-13 12:40:24
Message-ID: 411CB6B8.1050909@bitoek.uni-bayreuth.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Hi,

i'm trying to write a user-defined C function which creates a thumbnail
out of a base64 encoded string. The ImageMagick C++ API provides exactly
the functionality i need. So i would like to use these C++ functions.
I figured out that i have to define the entry point function with C
linkage (extern "C" { ... }).
Can anyone point me to example which calls a C++ function from a dynamic
library ?

--
Oliver Archner
Tel.: 0921/55-5727
Fax.: 0921/55-5799
BITÖK Wissenschaftliches Sekretariat
Universität Bayreuth
D-95440 Bayreuth


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Oliver Archner <oliver(dot)archner(at)bitoek(dot)uni-bayreuth(dot)de>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: Calling C++ function
Date: 2004-08-15 18:27:38
Message-ID: 28455.1092594458@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Oliver Archner <oliver(dot)archner(at)bitoek(dot)uni-bayreuth(dot)de> writes:
> Can anyone point me to example which calls a C++ function from a dynamic
> library ?

It's gonna be fairly painful, since the backend is a C program not a C++
program, and therefore it does not provide any of the infrastructure
that C++ code is likely to expect --- no exception stack, no global
constructors/destructors, no C++ library, etc. I am not sure how much
of this is practical to load after-the-fact in a dynamic library.

My inclination if you really need to use ImageMagick would be to shell
out to the IM executable via system() (or better popen(), if you have a
modern popen that supports 'r+' access).

regards, tom lane


From: Richard Welty <rwelty(at)averillpark(dot)net>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: Calling C++ function
Date: 2004-08-15 20:35:07
Message-ID: Mahogany-0.66.0-24736-20040815-163507.00@averillpark.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

On Sun, 15 Aug 2004 14:27:38 -0400 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Oliver Archner <oliver(dot)archner(at)bitoek(dot)uni-bayreuth(dot)de> writes:
> > Can anyone point me to example which calls a C++ function from a dynamic
> > library ?

> It's gonna be fairly painful, since the backend is a C program not a C++
> program, and therefore it does not provide any of the infrastructure
> that C++ code is likely to expect --- no exception stack, no global
> constructors/destructors, no C++ library, etc. I am not sure how much
> of this is practical to load after-the-fact in a dynamic library.

it can probably be pulled off. it will probably be very annoying to do.
we used to call gnu C++ from a Lucid Common Lisp environment back
at GE R&D (i was responsible for that particular integration can of
worms, it brings back painful memories, and it probably was a major
factor in my continuing dislike for C++.)

the library loads and initialization are an issue, as is figuring out the
"C Name" of the C++ functions. we used to run nm over the C++ object
files to find out what the C++ name mangler did. it was very clumsy,
and i spent a lot of time wishing for a better method.

tom's suggestion of shelling out might be a lot less painful.

richard
--
Richard Welty rwelty(at)averillpark(dot)net
Averill Park Networking 518-573-7592
Java, PHP, PostgreSQL, Unix, Linux, IP Network Engineering, Security