通过Cgo访问Aerospike C客户端时出错

时间:2018-10-08 09:08:24

标签: c go makefile cgo gccgo

我试图学习Cgo,所以我尝试从Cgo访问Aerospike客户

package main  
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include <string.h>
// #include "aerospike-client-c/examples/put/example_utils.h"
import "C"
import (
"unsafe"
)
func main() {  
   retvals := C.putitnew()
  _=retvals
}

但是我遇到了以下错误。 (请注意,当我执行make和make run时,C程序将成功运行)。

undefined reference to `example_get_opts'
./aerospike-client-c/examples/put/example.c:66: undefined reference to 
`example_connect_to_aerospike'
./aerospike-client-c/examples/put/example.c:69: undefined reference to 
`example_remove_test_record'
./aerospike-client-c/examples/put/example.c:78: undefined reference to 
  `as_record_init'
./aerospike-client-c/examples/put/example.c:79: undefined reference to 
`as_record_set_int64'
/tmp/go-build283334635/b046/_x002.o: In function `as_record_set_str':
....

因此,我认为问题在于Makefile中的配置。我已经搜索了一整天,并尝试了许多解决方案,但都没有成功。您能帮我如何在Cgo中导入Makefile吗?或其他可以帮助我成功执行此操作的方法。

1 个答案:

答案 0 :(得分:0)

您需要链接到相关的库。我相信该库称为-laerospike。在这种情况下,cgo指令将如下所示:

// #cgo LDFLAGS: -laerospike

请参见the cgo documentation

此外,您需要链接相关的示例代码。我在官方存储库中没有看到put的示例。您可能必须直接在Go文件的cgo部分中复制其部分来源,因为示例通常不打算直接链接。