이 튜토리얼을 기반으로 한 간단한 함수 인 PostgreSQL 확장을 작성하려고했습니다 . 작동하지만 GEOS 코드를 사용하는 확장을 작성하고 싶습니다. 그래서 Postgis 함수 ST_RelateMatch (하나 이상의 패턴 매개 변수에 대해) 에 따라 함수를 작성 했지만 컴파일 중에 문제가 발생했습니다 (아마도 링크와 관련).
이것은 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);
}
그런 다음 Makefile
튜토리얼에서 사용 했습니다.
MODULES = myrelatematch
EXTENSION = myrelatematch
DATA = myrelatematch--0.0.1.sql
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
그리고 이것은 내 SQL 코드입니다.
CREATE OR REPLACE FUNCTION
myrelatematch(text,variadic text[]) RETURNS int AS 'MODULE_PATHNAME','myrelatematch'
LANGUAGE C STRICT;
이제 make
Ubuntu 18.04에서 실행하면 오류가 없습니다. 공유 객체를 생성합니다. 실행하면 sudo make install
오류가 없지만 CREATE EXTENSION myrelatematch
데이터베이스에서 실행하면 다음 오류가 발생합니다.
ERROR: could not load library "/usr/lib/postgresql/11/lib/myrelatematch.so": /usr/lib/postgresql/11/lib/myrelatematch.so: undefined symbol: lwgeom_geos_error
Ubuntu 18.04를 실행 중이며 Postgis 용 모든 라이브러리가 서버에 있습니다 (소스에서 Postgis를 컴파일 할 수 있음). Postgresql 버전은 11.6 입니다.
나는 경험이 없으며 항상 소프트웨어의 더 큰 부분을 컴파일하는 데 문제가 있습니다. 라이브러리 연결에 문제가 있다고 생각했지만 하루 종일 디버깅을 마치고 도움이 필요합니다.
편집하다
gcc 명령을 복사하고 추가 -Wl,--no-undefined
하면 링커에서 오류가 발생합니다.
/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'
추가하면 -L/home/username/myrelatematch/postgis-3.0.0/liblwgeom
변경 사항이없고 오류는 동일합니다.
다음은 완전한 gcc 명령입니다.
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
이러한 질문을 기반으로 ( pgxs 질문-c 함수를 외부 라이브러리에 연결 하고 PGXS는 모듈을 연결할 때 SHLIB_LINK를 무시합니다 ) 솔루션을 찾았습니다.
다음은 타사 라이브러리가있는 Makefile의 예입니다.
그리고 내 메이크위한 GEOS
, 라이브러리 :LIBLWGEOM
LIBPGCOMMON
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)
나는 변화가 MODULES
에 대한 MODULE_big
다음 SHLIB_LINK
있도록 편집에 고려 -l
플래그 라이브러리를 사용할 수 있습니다. 그 후 libpgcommon
PostGIS에서 내 프로젝트로 복사 하고 PG_LDFLAGS
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다