简体   繁体   English

转到:导入和C库之间的类型冲突

[英]Go: conflicting types between import and C library

Trying to get acquainted with Go/C interop, I want to use git2go/libgit2 to read data of a git repository using a Redis backend . 为了熟悉Go / C互操作,我想使用git2go / libgit2使用Redis后端读取git存储库的数据。

So I came up with this code (stripped of error handling etc), which outputs a compile error that I cannot place: 所以我想出了这段代码(去除了错误处理等),它输出了我无法放置的编译错误:

./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

Apparently the compiler thinks that git.C.struct_git_odb_backend and C.struct_git_odb_backend are different types although they're the same - only one libgit2 on the system after all. 显然,编译器认为git.C.struct_git_odb_backendC.struct_git_odb_backend是相同的类型-毕竟系统上只有一个libgit2。 What can I do to resolve this? 我该怎么解决?

Here's the full listing: 这是完整的清单:

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)
}

I had the same issue when trying to split git2go into sub-packages. 尝试将git2go拆分为子包时,我遇到了同样的问题。 As far as I can tell, this is the Go compiler trying to use Go scoping rules to C code. 据我所知,这是Go编译器试图将Go范围规则应用于C代码。 I see two ways to work around this: 我看到两种方法可以解决此问题:

  1. Implement the git_odb interface in git2go so you can run arbitrary Go code; git_odb中实现git_odb接口,以便您可以运行任意Go代码; or 要么
  2. Write the code that adds the backend in C and call that from your Go code 编写在C语言中添加后端的代码,然后从Go代码中调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM