Go:导入和 C 库之间的类型冲突
Go: conflicting types between import and C library
尝试熟悉 Go/C 互操作,我想使用 git2go/libgit2 使用 Redis backend.
读取 git 存储库的数据
所以我想出了这段代码(去掉了错误处理等),它输出了一个我无法放置的编译错误:
./git.go:30: cannot use odbBackendC (type *C.struct_git_odb_backend) as type *git.C.struct_git_odb_backend in argument to git.NewOdbBackendFromC
显然编译器认为 git.C.struct_git_odb_backend
和 C.struct_git_odb_backend
是不同的类型,尽管它们是相同的——毕竟系统上只有一个 libgit2。我该怎么做才能解决这个问题?
完整列表如下:
package main
/*
#cgo LDFLAGS: -L ./libgit2-backends/redis -lgit2 -lhiredis -lgit2-redis
#include <git2.h>
extern int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password);
extern int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password);
*/
import "C"
import (
git "gopkg.in/libgit2/git2go.v23"
)
func ImportRepo(url string) {
odb, err := git.NewOdb();
var odbBackendC *C.git_odb_backend = nil
C.git_odb_backend_hiredis(&odbBackendC, C.CString("prefix_"), C.CString("path"), C.CString("localhost"), 6379, C.CString(""))
backend := git.NewOdbBackendFromC(odbBackendC)
odb.AddBackend(backend)
}
我在尝试将 git2go 拆分为子包时遇到了同样的问题。据我所知,这是 Go 编译器试图将 Go 作用域规则用于 C 代码。我看到有两种解决方法:
- 在 git2go 中实现
git_odb
接口,这样你就可以 运行 任意 Go 代码;或
- 用 C 编写添加后端的代码,并从您的 Go 代码中调用它
尝试熟悉 Go/C 互操作,我想使用 git2go/libgit2 使用 Redis backend.
读取 git 存储库的数据所以我想出了这段代码(去掉了错误处理等),它输出了一个我无法放置的编译错误:
./git.go:30: cannot use odbBackendC (type *C.struct_git_odb_backend) as type *git.C.struct_git_odb_backend in argument to git.NewOdbBackendFromC
显然编译器认为 git.C.struct_git_odb_backend
和 C.struct_git_odb_backend
是不同的类型,尽管它们是相同的——毕竟系统上只有一个 libgit2。我该怎么做才能解决这个问题?
完整列表如下:
package main
/*
#cgo LDFLAGS: -L ./libgit2-backends/redis -lgit2 -lhiredis -lgit2-redis
#include <git2.h>
extern int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password);
extern int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password);
*/
import "C"
import (
git "gopkg.in/libgit2/git2go.v23"
)
func ImportRepo(url string) {
odb, err := git.NewOdb();
var odbBackendC *C.git_odb_backend = nil
C.git_odb_backend_hiredis(&odbBackendC, C.CString("prefix_"), C.CString("path"), C.CString("localhost"), 6379, C.CString(""))
backend := git.NewOdbBackendFromC(odbBackendC)
odb.AddBackend(backend)
}
我在尝试将 git2go 拆分为子包时遇到了同样的问题。据我所知,这是 Go 编译器试图将 Go 作用域规则用于 C 代码。我看到有两种解决方法:
- 在 git2go 中实现
git_odb
接口,这样你就可以 运行 任意 Go 代码;或 - 用 C 编写添加后端的代码,并从您的 Go 代码中调用它