dongtang2376 2016-04-16 22:59
浏览 51
已采纳

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

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 <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)
}
  • 写回答

1条回答 默认 最新

  • dongliqin6939 2016-04-18 10:51
    关注

    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog