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

From: Isaac Morland <isaac(dot)morland(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Can I use extern "C" in an extension so I can use C++?
Date: 2020-07-05 20:53:49
Message-ID: CAMsGm5eOg-SaWKSHkytAnQ_YqfXf2nKxrs=H_Q1XGKcdaGh2DQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

The C++ compiler is mangling the names so they aren't visible to the
extension mechanism. The following page suggests using extern C (it doesn't
specify that this means extern "C", but I suppose anybody who actually knew
what they were doing would have known that immediately):

https://www.postgresql.org/docs/current/xfunc-c.html

So I'm writing my functions to start:

extern "C" Datum ...

The problem is that PG_FUNCTION_INFO_V1 generates its own function
declaration with a conflicting extern specification (just plain extern,
which I guess means "C++" in the context of C++ code).

I also tried wrapping everything - both the functions and
the PG_FUNCTION_INFO_V1 invocations - in extern "C" { ... }, but now a
variable declaration from fmgr.h conflicts with one from the macros.

Is there a simple fix I'm missing? Any hints much appreciated.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2020-07-05 21:00:30 Re: Online checksums verification in the backend
Previous Message Daniel Gustafsson 2020-07-05 20:48:42 Re: vacuum verbose detail logs are unclear; log at *start* of each stage