【问题标题】:PostgreSQL extension using GEOS and PostGIS - undefined symbol: lwgeom_geos_error使用 GEOS 和 PostGIS 的 PostgreSQL 扩展 - 未定义符号:lwgeom_geos_error
【发布时间】:2020-02-10 09:39:23
【问题描述】:

我尝试编写一些 PostgreSQL 扩展,只是一个基于 tutorial 的简单函数。它 工作,但后来我想写一个使用一些 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;

现在,如果我在 Ubuntu 18.04 上运行 make,则不会出现错误。它会创建共享对象,如果我运行 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

【问题讨论】:

  • 同样的问题,没有解决,但是: 1. 将“override CFLAGS := $(CFLAGS) -llwgeom”添加到 Makefile,拥有 lwgem 库应该会让你向前迈出一小步。 ...然后我正在尝试修改 Makefile 以指向已编译的库,抱歉不能做更多,分享我的 conf 只会增加混乱,我会做一些更相关的事情。

标签: c postgresql compilation postgis geos


【解决方案1】:

基于这些问题(pgxs question - linking c-functions to external librariesPGXS ignores SHLIB_LINK when linking modules)我找到了解决方案。

Here is the example of Makefile with third party libraries

还有我的 GEOSLIBLWGEOMLIBPGCOMMON 库的 Makefile:

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 链接。

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 2015-01-15
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2016-04-08
    • 2013-08-17
    • 1970-01-01
    相关资源
    最近更新 更多