From 92ff0ab38c864c2edea49a83ec0e46c33bf71e64 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 26/27] Add C linkage to client libraries in non-systematic
 ways

---
 src/backend/utils/mb/encnames.c     |  3 +++
 src/include/mb/pg_wchar.h           | 12 ++++++++++++
 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, 33 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/mb/encnames.c b/src/backend/utils/mb/encnames.c
index 11099b8..dc62e90 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 24e8d0d..f5dd098 100644
--- a/src/include/mb/pg_wchar.h
+++ b/src/include/mb/pg_wchar.h
@@ -491,11 +491,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 2607a3d..831e9be 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -460,8 +460,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 c0c3ac1..ad77b05 100644
--- a/src/interfaces/ecpg/pgtypeslib/dt.h
+++ b/src/interfaces/ecpg/pgtypeslib/dt.h
@@ -365,7 +365,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 0b4065e..d71b2c0 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 9ca0756..c8f5293 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -501,6 +501,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 ce87cd5..ba4ac74 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.9.3

