I tried to write some PostgreSQL extension, just a simple function based on this tutorial. It works but then I want to write an extension which uses some GEOS code. So I wrote function according to Postgis function ST_RelateMatch (but for more than one pattern parameter), but I have a problem during compilation (probably with linking).
This is my function written in C:
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "utils/builtins.h"
// here I had to add path to headers because I got fatal error: lwgeom_geos.h: No such file or directory
#include "./postgis-3.0.0/liblwgeom/lwgeom_geos.h"
#include "./postgis-3.0.0/libpgcommon/lwgeom_pg.h"
#include <string.h>
#include <assert.h>
/* #define POSTGIS_DEBUG_LEVEL 4 */
/* My RelateMatchFunction */
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(myrelatematch);
Datum myrelatematch(PG_FUNCTION_ARGS)
{
char *mat, *pat;
text *mat_text, *pat_text;
int result, arg;
mat_text = (PG_GETARG_TEXT_P(0));
mat = text_to_cstring(mat_text);
initGEOS(lwpgnotice, lwgeom_geos_error);
for (arg=1; arg < PG_NARGS(); arg++)
{
if (!PG_ARGISNULL(arg))
{
pat_text = (PG_GETARG_TEXT_P(arg));
pat = text_to_cstring(pat_text);
result = GEOSRelatePatternMatch(mat, pat);
if (result == 2)
{
lwfree(mat); lwfree(pat);
lwpgerror("GEOSRelatePatternMatch: %s", lwgeom_geos_errmsg);
PG_RETURN_NULL();
} else if (result == 1)
{
lwfree(mat); lwfree(pat);
PG_RETURN_BOOL(result);
}
}
}
PG_RETURN_BOOL(0);
}
Then I used Makefile
from the tutorial:
MODULES = myrelatematch
EXTENSION = myrelatematch
DATA = myrelatematch--0.0.1.sql
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
And this is my SQL code:
CREATE OR REPLACE FUNCTION
myrelatematch(text,variadic text[]) RETURNS int AS 'MODULE_PATHNAME','myrelatematch'
LANGUAGE C STRICT;
Now if I run make
on Ubuntu 18.04 there is no error. It creates shared object, if I run sudo make install
there is no error but if I run CREATE EXTENSION myrelatematch
in the database I got this error:
ERROR: could not load library "/usr/lib/postgresql/11/lib/myrelatematch.so": /usr/lib/postgresql/11/lib/myrelatematch.so: undefined symbol: lwgeom_geos_error
I am running Ubuntu 18.04, all libraries for Postgis are on the server (I am able to compile Postgis from source). Postgresql version is 11.6.
I am not experienced and I always find a problem with compilation some bigger part of software. I supposed there is a problem with linking the libraries but after all day of debugging I need a help.
EDIT
If I copy gcc command and add -Wl,--no-undefined
I get the errors from linker:
/home/username/myrelatematch/myrelatematch.c:26: undefined reference to `pg_detoast_datum'
/home/username/myrelatematch/myrelatematch.c:27: undefined reference to `text_to_cstring'
/home/username/myrelatematch/myrelatematch.c:29: undefined reference to `lwgeom_geos_error'
/home/username/myrelatematch/myrelatematch.c:29: undefined reference to `lwpgnotice'
/home/username/myrelatematch/myrelatematch.c:29: undefined reference to `initGEOS'
/home/username/myrelatematch/myrelatematch.c:35: undefined reference to `pg_detoast_datum'
/home/username/myrelatematch/myrelatematch.c:36: undefined reference to `text_to_cstring'
/home/username/myrelatematch/myrelatematch.c:38: undefined reference to `GEOSRelatePatternMatch'
/home/username/myrelatematch/myrelatematch.c:46: undefined reference to `lwfree'
/home/username/myrelatematch/myrelatematch.c:46: undefined reference to `lwfree'
/home/username/myrelatematch/myrelatematch.c:41: undefined reference to `lwfree'
/home/username/myrelatematch/myrelatematch.c:41: undefined reference to `lwfree'
/home/username/myrelatematch/myrelatematch.c:42: undefined reference to `lwgeom_geos_errmsg'
/home/username/myrelatematch/myrelatematch.c:42: undefined reference to `lwpgerror'
If I add -L/home/username/myrelatematch/postgis-3.0.0/liblwgeom
there is no change and the error is the same.
Here is the complete gcc command:
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer -fPIC -L/usr/lib/x86_64-linux-gnu -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -L/usr/lib/llvm-6.0/lib -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed -L/home/username/myrelatematch/postgis-3.0.0/liblwgeom -Wl,--no-undefined -shared -o myrelatematch.so myrelatematch.o
Based on these questions (pgxs question - linking c-functions to external libraries and PGXS ignores SHLIB_LINK when linking modules) I found solution.
Here is the example of Makefile with third party libraries
And my Makefile for GEOS
, LIBLWGEOM
a LIBPGCOMMON
libraries:
EXTENSION = vectgen
DATA = vectgen--0.0.1.sql
OBJS = vectgen.o
MODULE_big = vectgen
# postgres build stuff
SHLIB_LINK = -lgeos_c -llwgeom -lpgcommon
PG_LDFLAGS += -L /home/username/CLionProjects/test_geos/libpgcommon
PG_CPPFLAGS = -I /home/username/CLionProjects/test_geos/libpgcommon
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
I have to changes MODULES
for MODULE_big
, then SHLIB_LINK
is considered in compilation so -l
flag works for libraries. After that I copy libpgcommon
from PostGIS into my project and linked with PG_LDFLAGS
.