From e345664bbb2b6b25905a8fd4206e8ae1160af4ba Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Tue, 30 Aug 2016 12:00:00 -0400
Subject: [PATCH v2 20/23] Add C linkage to client libraries in non-systematic
 ways

---
 src/backend/utils/mb/encnames.c     |  3 +++
 src/include/mb/pg_wchar.h           | 14 ++++++++++++++
 src/include/port.h                  |  6 ++++++
 src/interfaces/ecpg/pgtypeslib/dt.h |  2 +-
 src/interfaces/libpq/Makefile       |  2 +-
 src/interfaces/libpq/libpq-fe.h     |  1 +
 src/interfaces/libpq/pqexpbuffer.h  |  9 +++++++++
 7 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/mb/encnames.c b/src/backend/utils/mb/encnames.c
index 11099b844f..dc62e90bf4 100644
--- a/src/backend/utils/mb/encnames.c
+++ b/src/backend/utils/mb/encnames.c
@@ -6,6 +6,9 @@
  */
 #ifdef FRONTEND
 #include "postgres_fe.h"
+#ifdef LIBPQ
+#include "libpq-fe.h"
+#endif
 #else
 #include "postgres.h"
 #include "utils/builtins.h"
diff --git a/src/include/mb/pg_wchar.h b/src/include/mb/pg_wchar.h
index ceb5695924..01a89185ec 100644
--- a/src/include/mb/pg_wchar.h
+++ b/src/include/mb/pg_wchar.h
@@ -437,9 +437,11 @@ typedef uint32 (*utf_local_conversion_func) (uint32 code);
  * These functions are considered part of libpq's exported API and
  * are also declared in libpq-fe.h.
  */
+extern "C" {
 extern int	pg_char_to_encoding(const char *name);
 extern const char *pg_encoding_to_char(int encoding);
 extern int	pg_valid_server_encoding_id(int encoding);
+}
 
 /*
  * Remaining functions are not considered part of libpq's API, though many
@@ -491,11 +493,23 @@ extern int	pg_bind_textdomain_codeset(const char *domainname);
 #endif
 
 extern int	pg_valid_client_encoding(const char *name);
+#ifdef LIBPQ
+extern "C" {
+#endif
 extern int	pg_valid_server_encoding(const char *name);
+#ifdef LIBPQ
+}
+#endif
 
 extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string);
 extern pg_wchar utf8_to_unicode(const unsigned char *c);
+#ifdef LIBPQ
+extern "C" {
+#endif
 extern int	pg_utf_mblen(const unsigned char *);
+#ifdef LIBPQ
+}
+#endif
 extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
 						  int src_encoding,
 						  int dest_encoding);
diff --git a/src/include/port.h b/src/include/port.h
index 30da8e7d90..82f3eec584 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -467,8 +467,14 @@ extern int	pg_check_dir(const char *dir);
 extern int	pg_mkdir_p(char *path, int omode);
 
 /* port/pqsignal.c */
+#ifdef LIBPQ
+extern "C" {
+#endif
 typedef void (*pqsigfunc) (int signo);
 extern pqsigfunc pqsignal(int signo, pqsigfunc func);
+#ifdef LIBPQ
+}
+#endif
 
 /* port/quotes.c */
 extern char *escape_single_quotes_ascii(const char *src);
diff --git a/src/interfaces/ecpg/pgtypeslib/dt.h b/src/interfaces/ecpg/pgtypeslib/dt.h
index dc711b914b..34f77d761f 100644
--- a/src/interfaces/ecpg/pgtypeslib/dt.h
+++ b/src/interfaces/ecpg/pgtypeslib/dt.h
@@ -327,7 +327,7 @@ void		GetCurrentDateTime(struct tm *);
 int			date2j(int, int, int);
 void		TrimTrailingZeros(char *);
 void		dt2time(double, int *, int *, int *, fsec_t *);
-int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
+extern "C" int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
 							int *year, int *month, int *day,
 							int *hour, int *minute, int *second,
 							int *tz);
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index 4b1e552d16..916f3d2533 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -19,7 +19,7 @@ NAME= pq
 SO_MAJOR_VERSION= 5
 SO_MINOR_VERSION= $(MAJORVERSION)
 
-override CPPFLAGS :=  -DFRONTEND -DUNSAFE_STAT_OK -I$(srcdir) $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port
+override CPPFLAGS :=  -DFRONTEND -DLIBPQ -DUNSAFE_STAT_OK -I$(srcdir) $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port
 ifneq ($(PORTNAME), win32)
 override CFLAGS += $(PTHREAD_CFLAGS)
 endif
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 635af5b50e..2416e5bbfe 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -505,6 +505,7 @@ extern void PQclear(PGresult *res);
 extern void PQfreemem(void *ptr);
 
 /* Exists for backward compatibility.  bjm 2003-03-24 */
+extern void PQfreeNotify(PGnotify *notify);
 #define PQfreeNotify(ptr) PQfreemem(ptr)
 
 /* Error when no password was given. */
diff --git a/src/interfaces/libpq/pqexpbuffer.h b/src/interfaces/libpq/pqexpbuffer.h
index 239def42b7..6cffc4cdd3 100644
--- a/src/interfaces/libpq/pqexpbuffer.h
+++ b/src/interfaces/libpq/pqexpbuffer.h
@@ -25,6 +25,11 @@
 #ifndef PQEXPBUFFER_H
 #define PQEXPBUFFER_H
 
+#ifdef __cplusplus
+extern      "C"
+{
+#endif
+
 /*-------------------------
  * PQExpBufferData holds information about an extensible string.
  *		data	is the current buffer for the string (allocated with malloc).
@@ -179,4 +184,8 @@ extern void appendPQExpBufferChar(PQExpBuffer str, char ch);
 extern void appendBinaryPQExpBuffer(PQExpBuffer str,
 						const char *data, size_t datalen);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif   /* PQEXPBUFFER_H */
-- 
2.12.0

