Linking PostgreSQL as a C++ program

From: Craig Ringer <craig(at)2ndquadrant(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Linking PostgreSQL as a C++ program
Date: 2018-01-29 04:46:54
Message-ID: CAMsr+YE1YhYVhvvB_NaZhpb3wDntyaQzxiOE8RSEM8gMxJfEYA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all

In my ongoing efforts to make Tom look at me in horror, I've compiled
PostgreSQL with C++ objects linked into the core server. Currently this is
just my notes on how, in case anyone else needs to later.

To do it you really only have to change src/backend/Makefile to use g++ as
a linker:

postgres: $(OBJS)
- $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(call
expand_subsys,$^) $(LIBS) -o $@
+ $(CXX) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(call
expand_subsys,$^) $(LIBS) -o $@

so it links to libstdc++.

To use the postgres headers within C++ code you must also:

extern "C" {
#include "postgres.h"
}

since we only do that automagically for the frontend headers.

Currently it's also necessary to add a "_THROW" annotation to
src/include/port.h's definition of inet_net_ntop to stop it conflicting
with <inet/arpa.h>'s definition.

The Makefiles already support building and linking C++ objects, so a normal
Makefile can specify c++ sources in "OBJS", e.g. "blah.o" will get compiled
from "blah.cpp".

If what I'm prototyping works out I'll do my best to move it into an
extension, because I can just imagine how likely a patch that adds c++11
code to the core server would be to get accepted ;)

--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2018-01-29 04:48:26 Re: Failed test 'psql query died successfully after SIGQUIT'
Previous Message Michael Paquier 2018-01-29 04:43:48 Re: [HACKERS] GnuTLS support