Go:导入和C库之间存在冲突的类型

huangapple go评论93阅读模式
英文:

Go: conflicting types between import and C library

问题

尝试熟悉Go/C互操作性,我想使用git2go/libgit2来使用Redis后端读取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_backendC.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)
}

希望对你有所帮助!

英文:

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.

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. 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 &lt;git2.h&gt;

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 &quot;C&quot;

import (
  git &quot;gopkg.in/libgit2/git2go.v23&quot;
)

func ImportRepo(url string) {
  odb, err := git.NewOdb();

  var odbBackendC *C.git_odb_backend = nil
  C.git_odb_backend_hiredis(&amp;odbBackendC, C.CString(&quot;prefix_&quot;), C.CString(&quot;path&quot;), C.CString(&quot;localhost&quot;), 6379, C.CString(&quot;&quot;))
  backend := git.NewOdbBackendFromC(odbBackendC)
  odb.AddBackend(backend)
}

答案1

得分: 2

当我尝试将git2go拆分为子包时,我遇到了同样的问题。据我所知,这是Go编译器试图将Go作用域规则应用于C代码。我看到两种解决方法:

  1. 在git2go中实现git_odb接口,这样你就可以运行任意的Go代码;
  2. 在C中编写添加后端的代码,并从你的Go代码中调用它。
英文:

I had the same issue when trying to split git2go into sub-packages. As far as I can tell, this is the Go compiler trying to use Go scoping rules to C code. I see two ways to work around this:

  1. Implement the git_odb interface in git2go so you can run arbitrary Go code; or
  2. Write the code that adds the backend in C and call that from your Go code

huangapple
  • 本文由 发表于 2016年4月17日 06:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/36670761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定