c - 通过Cgo访问aerospike C客户端报错

标签 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 吗?或者帮助我成功执行此操作的替代方法..

最佳答案

您需要链接到相关的图书馆。我相信这个库叫做 -laerospike。在这种情况下,cgo 指令将如下所示:

// #cgo LDFLAGS: -laerospike

参见 the cgo documentation .

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

关于c - 通过Cgo访问aerospike C客户端报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52698861/

相关文章:

c - 我应该如何释放指向字符串的指针数组?

php - 如何中止 makefile 中的 shell 命令错误?

pointers - Go中接口(interface)类型的间接

go - 为什么我的两个 slice 之一会因“运行时错误:索引超出范围”而感到 panic ?

mysql - 添加依赖项以生成文件 C++

c - 使文件对预处理器定义敏感

c - 我的 Fortran 编译器的中间代码生成 : DAG or quadruples

c - 在 ld 中明确指定符号的位置

c - 理解gnu libc的strcmp函数

sql - 如何在go中使用sql server包