首页 > 解决方案 > 为 PostgreSQL 9.6 编译 C 函数时出错

问题描述

我正在升级到 PostgreSQL 9.6 并在尝试编译一些 C 代码时遇到一些错误。

gcc -c -o lib/libhaver.o src/libhaver.c -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fPIC -D_GNU_SOURCE -I. -I/usr/pgsql-9.6/include/server -I/usr/pgsql-9.6/include/server/access -I/usr/pgsql-9.6/include/internal -I/usr/include/et -I/usr/include/libxml2 -I/usr/include

这段代码:

#include "postgres.h"
#include "fmgr.h"

#include "catalog/pg_type.h"
#include "funcapi.h"

#include "utils/guc.h"
#include "access/htup.h"
#include "utils/array.h"

#include "math.h"


#ifdef NEWPOSTGRES
#include "access/htup_details.h"
#endif


PG_MODULE_MAGIC;


// Define a missing value that we can insert into our array
# define FLOAT_NaN (0.0/0.0)

int32 mapAtoB(int32 i, int32 a2, int32 a1); 
PG_FUNCTION_INFO_V1( mapAtoB );
int32 mapAtoB(int32 i, int32 a2, int32 a1){
    int32 j = (i - (a1-a2));
    return j;
}

但是我收到此错误:

src/libhaver.c:30: error: conflicting types for ‘mapAtoB’
src/libhaver.c:29: note: previous declaration of ‘mapAtoB’ was here
make: *** [lib/libhaver.o] Error 1

它适用于 9.2 但不适用于 9.6...我做错了什么?

标签: cpostgresql

解决方案


看起来您正在使用“版本 0 调用约定”

应该在 9.6 中有效,但在 PostgreSQL v10 中删除了对它的支持。

切换到版本 1 调用约定:

PG_FUNCTION_INFO_V1(mapAtoB);

Datum
mapAtoB(PG_FUNCTION_ARGS)
{
    int32   i  = PG_GETARG_INT32(0);
    int32   a2 = PG_GETARG_INT32(1);
    int32   a1 = PG_GETARG_INT32(2);

    PG_RETURN_INT32(i - (a1-a2));
}

推荐阅读