c - "expected expression before ' 在 C 中构建 Postgres 函数时从 fmgr.h 返回 '"错误

标签 c postgresql

我正在尝试编译用于​​ Postgres 9.5 的 C 函数。当我尝试运行 makefile 来编译代码时,我收到以下消息/错误:

cc -o int_to_id.o -c int_to_id.c  -I /usr/include/postgresql/9.5/server
In file included from int_to_id.c:1:0:
int_to_id.c: In function ‘int_to_id’:
/usr/include/postgresql/9.5/server/postgres.h:613:26: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
 #define DatumGetInt64(X) ((int64) GET_8_BYTES(X))
                      ^
/usr/include/postgresql/9.5/server/fmgr.h:238:29: note: in expansion of macro ‘DatumGetInt64’
 #define PG_GETARG_INT64(n)  DatumGetInt64(PG_GETARG_DATUM(n))
                         ^
int_to_id.c:55:15: note: in expansion of macro ‘PG_GETARG_INT64’
 long *x = PG_GETARG_INT64(0);
           ^
In file included from int_to_id.c:2:0:
/usr/include/postgresql/9.5/server/fmgr.h:305:30: error: expected expression before ‘return’
 #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
                          ^
/usr/include/postgresql/9.5/server/fmgr.h:316:32: note: in expansion of macro ‘PG_RETURN_POINTER’
 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
                            ^
int_to_id.c:60:12: note: in expansion of macro ‘PG_RETURN_VARCHAR_P’
 return PG_RETURN_VARCHAR_P(cstring_to_text(result));
        ^
Makefile:15: recipe for target 'int_to_id.o' failed
make: *** [int_to_id.o] Error 1
看起来这些错误之一来自“fmgr.h”的语法,我不确定如何理解它,因为这是一个包含的库文件。我该如何解决这个错误?
作为引用,我的makefile:
MODULES = int_to_id

PG_CONFIG = pg_config
PGXS = $(shell $(PG_CONFIG) --pgxs)
INCLUDEDIR = $(shell $(PG_CONFIG) --includedir-server)
include $(PGXS)

int_to_id.so: int_to_id.o
    cc -shared -o int_to_id.so int_to_id.o

int_to_id.o: int_to_id.c
    cc -o int_to_id.o -c int_to_id.c $(CRFLAGS) -I $(INCLUDEDIR)
也供引用,C 源文件:
#include "postgres.h"
#include "fmgr.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

const char charmap[36] = {
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'q',
    'r',
    's',
    't',
    'u',
    'v',
    'w',
    'x',
    'y',
    'z'
};

Datum int_to_id(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(int_to_id);

Datum int_to_id(PG_FUNCTION_ARGS){
    char result[10] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    long base_val = 1L;
    long *x = PG_GETARG_INT64(0);
    for (int i = 1; i <= 10, i++; ){
        result[10 - i] = charmap[ ((*x) / base_val) % 36];
    }
    base_val = base_val * 36L;

    return PG_RETURN_VARCHAR_P(cstring_to_text(result));
}

最佳答案

第一个错误PG_GETARG_INT64(0)返回 Datum值作为 int64 .您正在将该值分配给一个指针 ( long * ),这使编译器不满意。
如果您知道自己在做什么,则可以通过强制转换来强制分配:

long *x = (long *)PG_GETARG_INT64(0);
但这对我来说看起来很可疑,并且使用 PostgresQL 类型和宏可能有更好的方法来做到这一点。
例如,这看起来会更好:
int64 x = PG_GETARG_INT64(0);
for (int i = 1; i++; i <= 10){
    result[10 - i] = charmap[(x / 36^i) % 36];
}
第二个错误
return PG_RETURN_VARCHAR_P(cstring_to_text(result));
最终被扩展为
return return PointerGetDatum(cstring_to_text(result))
这显然是错误的。去除多余的 return :
PG_RETURN_VARCHAR_P(cstring_to_text(result));

关于c - "expected expression before ' 在 C 中构建 Postgres 函数时从 fmgr.h 返回 '"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66412400/

相关文章:

sql - 触发器-postgres

Django:按月分组后只计算最新的对象

database - PostgreSQL 数据库中的条件索引

objective-c - 指针差异

c - 椭圆曲线离散对数

c - 为什么我的 C 程序可以在 "git bash"运行,但不能在 "cmd"运行?

postgresql - 如何在OpenERP的CRM模块中添加只有整数的手机号码

c - TAB 和 KEY_UP 之类的键在 PTY 程序中无法按预期工作(例如 KEY_UP 变为 "^[[A")

c - 我如何使用 vfork 在 uClinux 中生成守护进程?

c - Libevent 2.0.22 项目编译问题 (OSX)