英文:
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_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)
}
希望对你有所帮助!
英文:
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
得分: 2
当我尝试将git2go拆分为子包时,我遇到了同样的问题。据我所知,这是Go编译器试图将Go作用域规则应用于C代码。我看到两种解决方法:
- 在git2go中实现
git_odb
接口,这样你就可以运行任意的Go代码; - 在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:
- Implement the
git_odb
interface in git2go so you can run arbitrary Go code; or - Write the code that adds the backend in C and call that from your Go code
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论