Linking PostgreSQL as a C++ program - Mailing list pgsql-hackers

From Craig Ringer
Subject Linking PostgreSQL as a C++ program
Date
Msg-id CAMsr+YE1YhYVhvvB_NaZhpb3wDntyaQzxiOE8RSEM8gMxJfEYA@mail.gmail.com
Whole thread Raw
Responses Re: Linking PostgreSQL as a C++ program
List 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

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: [HACKERS] GnuTLS support
Next
From: Thomas Munro
Date:
Subject: Re: Failed test 'psql query died successfully after SIGQUIT'