From a3124209270fde6c7cc1fd00244eb968326b7e1f 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 25/27] Add C linkage to functions exported by plugins

This is required so dlsym() can find them reliably.
---
 contrib/bloom/bloom.h                           | 2 +-
 contrib/earthdistance/earthdistance.c           | 2 +-
 contrib/hstore/hstore.h                         | 4 ++--
 contrib/ltree/ltree.h                           | 2 ++
 contrib/pg_stat_statements/pg_stat_statements.c | 2 ++
 contrib/seg/seg.c                               | 6 ++++++
 src/include/fmgr.h                              | 6 +++---
 src/pl/plperl/plperl.c                          | 2 ++
 src/pl/plpgsql/src/plpgsql.h                    | 2 +-
 src/pl/plpython/plpy_main.c                     | 2 ++
 src/pl/tcl/pltcl.c                              | 2 ++
 src/test/regress/regress.c                      | 8 +++++---
 12 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/contrib/bloom/bloom.h b/contrib/bloom/bloom.h
index 18f5127..c80d9b0 100644
--- a/contrib/bloom/bloom.h
+++ b/contrib/bloom/bloom.h
@@ -173,7 +173,7 @@ typedef struct BloomScanOpaqueData
 typedef BloomScanOpaqueData *BloomScanOpaque;
 
 /* blutils.c */
-extern void _PG_init(void);
+extern "C" void _PG_init(void);
 extern void initBloomState(BloomState *state, Relation index);
 extern void BloomFillMetapage(Relation index, Page metaPage);
 extern void BloomInitMetapage(Relation index);
diff --git a/contrib/earthdistance/earthdistance.c b/contrib/earthdistance/earthdistance.c
index 861b166..305be5d 100644
--- a/contrib/earthdistance/earthdistance.c
+++ b/contrib/earthdistance/earthdistance.c
@@ -112,7 +112,7 @@ geo_distance(PG_FUNCTION_ARGS)
 }
 #else							/* !USE_FLOAT8_BYVAL */
 
-double	   *geo_distance(Point *pt1, Point *pt2);
+extern "C" double	   *geo_distance(Point *pt1, Point *pt2);
 
 double *
 geo_distance(Point *pt1, Point *pt2)
diff --git a/contrib/hstore/hstore.h b/contrib/hstore/hstore.h
index 6bab08b..373a5f1 100644
--- a/contrib/hstore/hstore.h
+++ b/contrib/hstore/hstore.h
@@ -194,8 +194,8 @@ extern Pairs *hstoreArrayToPairs(ArrayType *a, int *npairs);
 #if HSTORE_POLLUTE_NAMESPACE
 #define HSTORE_POLLUTE(newname_,oldname_) \
 	PG_FUNCTION_INFO_V1(oldname_);		  \
-	Datum newname_(PG_FUNCTION_ARGS);	  \
-	Datum oldname_(PG_FUNCTION_ARGS) { return newname_(fcinfo); } \
+	extern "C" Datum newname_(PG_FUNCTION_ARGS);	  \
+	extern "C" Datum oldname_(PG_FUNCTION_ARGS) { return newname_(fcinfo); } \
 	extern int no_such_variable
 #else
 #define HSTORE_POLLUTE(newname_,oldname_) \
diff --git a/contrib/ltree/ltree.h b/contrib/ltree/ltree.h
index c604357..277b9a4 100644
--- a/contrib/ltree/ltree.h
+++ b/contrib/ltree/ltree.h
@@ -130,6 +130,7 @@ typedef struct
 
 
 /* use in array iterator */
+extern "C" {
 Datum		ltree_isparent(PG_FUNCTION_ARGS);
 Datum		ltree_risparent(PG_FUNCTION_ARGS);
 Datum		ltq_regex(PG_FUNCTION_ARGS);
@@ -154,6 +155,7 @@ Datum		ltree_textadd(PG_FUNCTION_ARGS);
 
 /* Util function */
 Datum		ltree_in(PG_FUNCTION_ARGS);
+}
 
 bool ltree_execute(ITEM *curitem, void *checkval,
 			  bool calcnot, bool (*chkcond) (void *checkval, ITEM *val));
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 37aac98..fb6bd82 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -275,8 +275,10 @@ static bool pgss_save;			/* whether to save stats across shutdown */
 
 /*---- Function declarations ----*/
 
+extern "C" {
 void		_PG_init(void);
 void		_PG_fini(void);
+}
 
 PG_FUNCTION_INFO_V1(pg_stat_statements_reset);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_2);
diff --git a/contrib/seg/seg.c b/contrib/seg/seg.c
index c6c082b..45d3503 100644
--- a/contrib/seg/seg.c
+++ b/contrib/seg/seg.c
@@ -47,6 +47,7 @@ PG_FUNCTION_INFO_V1(seg_center);
 /*
 ** GiST support methods
 */
+extern "C" {
 bool gseg_consistent(GISTENTRY *entry,
 				SEG *query,
 				StrategyNumber strategy,
@@ -61,11 +62,13 @@ bool		gseg_internal_consistent(SEG *key, SEG *query, StrategyNumber strategy);
 SEG		   *gseg_union(GistEntryVector *entryvec, int *sizep);
 SEG		   *gseg_binary_union(SEG *r1, SEG *r2, int *sizep);
 bool	   *gseg_same(SEG *b1, SEG *b2, bool *result);
+}
 
 
 /*
 ** R-tree support functions
 */
+extern "C" {
 bool		seg_same(SEG *a, SEG *b);
 bool		seg_contains_int(SEG *a, int *b);
 bool		seg_contains_float4(SEG *a, float4 *b);
@@ -80,16 +83,19 @@ bool		seg_over_right(SEG *a, SEG *b);
 SEG		   *seg_union(SEG *a, SEG *b);
 SEG		   *seg_inter(SEG *a, SEG *b);
 void		rt_seg_size(SEG *a, float *sz);
+}
 
 /*
 ** Various operators
 */
+extern "C" {
 int32		seg_cmp(SEG *a, SEG *b);
 bool		seg_lt(SEG *a, SEG *b);
 bool		seg_le(SEG *a, SEG *b);
 bool		seg_gt(SEG *a, SEG *b);
 bool		seg_ge(SEG *a, SEG *b);
 bool		seg_different(SEG *a, SEG *b);
+}
 
 /*
 ** Auxiliary funxtions
diff --git a/src/include/fmgr.h b/src/include/fmgr.h
index 0491e2e..191235f 100644
--- a/src/include/fmgr.h
+++ b/src/include/fmgr.h
@@ -348,8 +348,8 @@ typedef const Pg_finfo_record *(*PGFInfoFunction) (void);
  *	doesn't hurt to add PGDLLIMPORT in case they don't.
  */
 #define PG_FUNCTION_INFO_V1(funcname) \
-Datum funcname(PG_FUNCTION_ARGS); \
-extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
+extern "C" Datum funcname(PG_FUNCTION_ARGS); \
+extern "C" PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
 const Pg_finfo_record * \
 CppConcat(pg_finfo_,funcname) (void) \
 { \
@@ -418,7 +418,7 @@ typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void);
 #define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func"
 
 #define PG_MODULE_MAGIC \
-extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
+extern "C" PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
 const Pg_magic_struct * \
 PG_MAGIC_FUNCTION_NAME(void) \
 { \
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 2cd7614..c5c0617 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -244,7 +244,9 @@ static plperl_call_data *current_call_data = NULL;
 /**********************************************************************
  * Forward declarations
  **********************************************************************/
+extern "C" {
 void		_PG_init(void);
+}
 
 static PerlInterpreter *plperl_init_interp(void);
 static void plperl_destroy_interp(PerlInterpreter **);
diff --git a/src/pl/plpgsql/src/plpgsql.h b/src/pl/plpgsql/src/plpgsql.h
index b416e50..f2ddd94 100644
--- a/src/pl/plpgsql/src/plpgsql.h
+++ b/src/pl/plpgsql/src/plpgsql.h
@@ -1085,7 +1085,7 @@ extern void plpgsql_HashTableInit(void);
 /*
  * Functions in pl_handler.c
  */
-extern void _PG_init(void);
+extern "C" void _PG_init(void);
 
 /*
  * Functions in pl_exec.c
diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c
index 860b804..efd3bf0 100644
--- a/src/pl/plpython/plpy_main.c
+++ b/src/pl/plpython/plpy_main.c
@@ -39,7 +39,9 @@
 #define plpython_inline_handler plpython3_inline_handler
 #endif
 
+extern "C" {
 extern void _PG_init(void);
+}
 
 PG_MODULE_MAGIC;
 
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 2a335aa..20578ad 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -205,7 +205,9 @@ static const TclExceptionNameMap exception_name_map[] = {
 /**********************************************************************
  * Forward declarations
  **********************************************************************/
+extern "C" {
 void		_PG_init(void);
+}
 
 static void pltcl_init_interp(pltcl_interp_desc *interp_desc, bool pltrusted);
 static pltcl_interp_desc *pltcl_fetch_interp(bool pltrusted);
diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c
index e7826a4..9621cc1 100644
--- a/src/test/regress/regress.c
+++ b/src/test/regress/regress.c
@@ -45,8 +45,8 @@
 
 extern PATH *poly2path(POLYGON *poly);
 extern void regress_lseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
-extern char *reverse_name(char *string);
-extern int	oldstyle_length(int n, text *t);
+extern "C" char *reverse_name(char *string);
+extern "C" int	oldstyle_length(int n, text *t);
 
 #ifdef PG_MODULE_MAGIC
 PG_MODULE_MAGIC;
@@ -240,8 +240,10 @@ typedef struct
 	double		radius;
 } WIDGET;
 
+extern "C" {
 WIDGET	   *widget_in(char *str);
-char	   *widget_out(WIDGET *widget);
+char	   *widget_out(WIDGET * widget);
+}
 
 #define NARGS	3
 
-- 
2.9.3

