dtxs9017 2017-01-04 05:27
浏览 93
已采纳

如何使用cgo构建Postgres扩展

Here is what I'm doing right now,

.
├── helloworld--1.0.sql
├── helloworld.control
├── helloworld.go
└── Makefile

helloworld.go :

package helloworld

/*
#cgo LDFLAGS: -rdynamic
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(helloworld);
PG_FUNCTION_INFO_V1(hello_text_arg);
PG_FUNCTION_INFO_V1(hello_ereport);

Datum
hello_world(PG_FUNCTION_ARGS)
{
    PG_RETURN_TEXT_P(cstring_to_text("Hello, World!"));
}

Datum
hello_text_arg(PG_FUNCTION_ARGS)
{
    text *hello     = cstring_to_text("Hello, ");
    int32 hello_sz  = VARSIZE(hello) - VARHDRSZ;

    text *name      = PG_GETARG_TEXT_P(0);
    int32 name_sz   = VARSIZE(name) - VARHDRSZ;

    text *tail      = cstring_to_text("!");
    int32 tail_sz   = VARSIZE(tail) - VARHDRSZ;

    int32 out_sz    = hello_sz + name_sz + tail_sz + VARHDRSZ;
    text *out       = (text *) palloc(out_sz);

    SET_VARSIZE(out, out_sz);

    memcpy(VARDATA(out), VARDATA(hello), hello_sz);
    memcpy(VARDATA(out) + hello_sz, VARDATA(name), name_sz);
    memcpy(VARDATA(out) + hello_sz + name_sz, VARDATA(tail), tail_sz);

    PG_RETURN_TEXT_P(out);
}

Datum
hello_ereport(PG_FUNCTION_ARGS)
{
    ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("null value not allowed")));

    PG_RETURN_VOID();
}
*/
import "C"

Makefile :

MODULES = helloworld

EXTENSION = helloworld
DATA = helloworld--1.0.sql
PGFILEDESC = "helloworld - example extension for postgresql"

REGRESS = helloworld

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

helloworld.so:
    CGO_CFLAGS="-rdynamic -I$(INCLUDEDIR)" CGO_LDFLAGS="-rdynamic $(LDFLAGS)" go build -v -buildmode=c-shared -o helloworld.so .

It produced these errors when making :

/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_world':
helloworld.cgo2.c:(.text+0x48): undefined reference to `cstring_to_text'
/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_text_arg':
helloworld.cgo2.c:(.text+0x63): undefined reference to `cstring_to_text'
helloworld.cgo2.c:(.text+0x86): undefined reference to `pg_detoast_datum'
helloworld.cgo2.c:(.text+0xa5): undefined reference to `cstring_to_text'
helloworld.cgo2.c:(.text+0xd7): undefined reference to `palloc'
/tmp/go-build019341122/github.com/amosbird/rpctest/helloworld/_obj/helloworld.cgo2.o: In function `hello_ereport':
helloworld.cgo2.c:(.text+0x1a8): undefined reference to `errstart'
helloworld.cgo2.c:(.text+0x1bd): undefined reference to `errmsg'
helloworld.cgo2.c:(.text+0x1c9): undefined reference to `errcode'
helloworld.cgo2.c:(.text+0x1d7): undefined reference to `errfinish'
collect2: ld returned 1 exit status
make: *** [helloworld.so] Error 2

I have no idea if this would work. There are so many black magic happened here.

So the main question is, what is the correct Makefile that can be used to build a Postgres extension in cgo?

A specific question to these errors is, what can I do to defer those symbol resolution in cgo's linking process?

  • 写回答

1条回答 默认 最新

  • dqcwl02022 2017-01-04 14:32
    关注

    Ok, I spent a whole day and found a viable solution.

    We need to have the extension's main source file start with:

    package main  // make sure to use main package
    
    /*
    #cgo CFLAGS: -I/path/to/postgres/include/server
    #cgo LDFLAGS: -Wl,-unresolved-symbols=ignore-all
    

    Use go build -o myext.so -buildmode=c-shared myext.go to generate myext.so.

    If some Go method is needed from the C side, we should add //export methodname above the method declaration. This will generate symbols without package name prefix. Then we can extern these symbols on the C side. Make sure the exported Go methods reside in packages other than main.

    package test :

    //export Merge
    func Merge(cint C.int) C.int ...
    

    package main :

    extern int Merge(int);
    
    import "./test"
    var _ = test.Somevar  // dumb placeholder to fake use package test.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?