Re: Can I use extern "C" in an extension so I can use C++?

From: Isaac Morland <isaac(dot)morland(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Can I use extern "C" in an extension so I can use C++?
Date: 2020-07-05 22:41:47
Message-ID: CAMsGm5dc-cXmqFeQcT1kEA3r-R0+8V2ERh2GAkDyN30peqUroA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, 5 Jul 2020 at 18:07, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Isaac Morland <isaac(dot)morland(at)gmail(dot)com> writes:
> > I'm writing a small extension, and I'm trying to use C++ constructs. I'm
> > not actually doing anything that needs C++, but I *really* like declaring
> > variables when I first initialize them (for example), and I also *really*
> > like warning-free compiles.
>
> Well, you could get that with -Wno-declaration-after-statement ...
> but yeah, this is supposed to work, modulo all the caveats on the
> page you already found.
>
> > The C++ compiler is mangling the names so they aren't visible to the
> > extension mechanism.
>
> Something like the attached works for me; what problem are you having
> *exactly*?
>

I've attached a .cpp file. When I run "make", I get:

g++ -Wall -Wpointer-arith -Wendif-labels -Wmissing-format-attribute
-Wformat-security -fno-strict-aliasing -fwrapv -g -g -O2
-fstack-protector-strong -Wformat -Werror=format-security -I. -I./
-I/usr/include/postgresql/12/server -I/usr/include/postgresql/internal
-Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2
-I/usr/include/mit-krb5 -c -o hashblob.o hashblob.cpp
hashblob.cpp:9:18: error: conflicting declaration of ‘Datum
hashblob_touch(FunctionCallInfo)’ with ‘C’ linkage
9 | extern "C" Datum hashblob_touch (PG_FUNCTION_ARGS) {
| ^~~~~~~~~~~~~~
In file included from hashblob.cpp:2:
hashblob.cpp:6:21: note: previous declaration with ‘C++’ linkage
6 | PG_FUNCTION_INFO_V1(hashblob_touch);
| ^~~~~~~~~~~~~~
/usr/include/postgresql/12/server/fmgr.h:405:14: note: in definition of
macro ‘PG_FUNCTION_INFO_V1’
405 | extern Datum funcname(PG_FUNCTION_ARGS); \
| ^~~~~~~~
[... and then the same set of 3 errors again for the other function ...]

Without the extern "C" stuff it compiles fine but the names are mangled;
renaming to .c makes it compile fine with non-mangled names. It also
compiles if I get rid of the PG_FUNCTION_INFO_V1 invocations; but the same
documentation page is pretty clear that is supposed to be needed; and then
I think it C++-mangles the names of functions that get called behind the
scenes ("undefined symbol: _Z23pg_detoast_datum_packedP7varlena" when I try
to CREATE EXTENSION).

I should add I'm using a Makefile I've also attached; it uses PGXS with
nothing special, but on the other hand it means I don't really understand
everything that is going on (in particular, I didn't pick whether to say
g++ or gcc, nor did I explicitly choose any of the arguments to the
command).

$ g++ -Wall -fno-strict-aliasing -fwrapv -g -O2 -D_GNU_SOURCE -c
> -I/home/postgres/pgsql/src/include -o test.o test.cpp
> $ nm --ext --def test.o
> 0000000000000000 T Pg_magic_func
> 0000000000000010 T pg_finfo_silly_func
> 0000000000000020 T silly_func
>

Attachment Content-Type Size
hashblob.cpp application/octet-stream 441 bytes
Makefile application/octet-stream 142 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2020-07-05 22:49:56 Re: Can I use extern "C" in an extension so I can use C++?
Previous Message Tom Lane 2020-07-05 22:07:07 Re: Can I use extern "C" in an extension so I can use C++?