我有两个项目,第一个,名为A,有一个子模块a导入sqlite3(github.com/mattn/go-sqlite3)。另一个B项目导入A的子模块a,在另一个子模块b中,它也导入相同的sqlite3。
A和B都将导入放在vendor dir下(由govendor管理)。我的歌朗版本是go version go1.12 linux/amd64。
当构建B (go build main.go)时,抛出以下错误(太多,其中一部分):
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/tmp/go-link-281256755/000029.o: In function `callbackTrampoline':
/tmp/go-build/_cgo_export.c:25: multiple definition of `callbackTrampoline'
/tmp/go-link-281256755/000005.o:/tmp/go-build/_cgo_export.c:25: first defined here
/tmp/go-link-281256755/000029.o: In function `stepTrampoline':
...
/home/xxx/go/src/gitlab.xxxxxxxxx.com/xxxxxxxxx-tools/A/vendor/github.com/mattn/go-sqlite3/sqlite3.go:129: multiple definition of `_sqlite3_result_text'
/tmp/go-link-281256755/000009.o:/home/xxx/go/src/gitlab.xxxxxxxxx.com/xxxxxxxxx-tools/A/vendor/github.com/mattn/go-sqlite3/sqlite3.go:129: first defined here
/tmp/go-link-281256755/000033.o: In function `_sqlite3_result_blob':
...但是构建A很好。为了测试错误,我开始跟随演示,也使用由vendor创建的govendor,并构建ok。
package main
import (
"database/sql"
"fmt"
"gitlab.xxxxxxxxx.com/xxxxxxxxxxxxxxx/A/a"
_ "github.com/mattn/go-sqlite3"
)
func main() {
fmt.Println(a.ModuleVariable) // use submodule `a` just like B is doing
_, _ = sql.Open(`sqlite3`, `test.db`) // use sqlite too
}我认为编译器首先编译A的sqlite3,对象是在/tmp/go-link-281256755/000005.o下创建的(但在构建后没有这个dir ),然后编译B的sqlite3导入并创建一个包含同名函数的对象,然后编译器找到两个同名符号,链接失败。
如何解决这些问题?是否有任何戈朗env设置来避免这些?
在我删除了vendor下的A和B下的sqlite3包之后,他们都使用~/go/src/github.com/mattn/go-sqlite3/下的sqlite3,他们都构建了ok。但是我不能这样做,因为项目A的部署平台,我必须把所有的依赖关系放在供应商之下,还有其他选择来使用同一个包的多个导入吗?
发布于 2020-11-12 02:01:03
对于cgo的链接错误“.的多重定义”问题,(工作)解决方案取决于链接C代码的性质:
--allow-multiple-definition传递给链接器(参见 man page)
go build --ldflags '-extldflags "-Wl,--allow-multiple-definition"',
或者通过连接到C代码的包的Go源中的#cgo指令:
//#cgo:-Wl,-允许-多定义导入"C“- Make sure to put keyword `static` to all declarations whose usage is within that C object only (not intended to be linked to Go nor to other C objects).
- Find some way to do _name mangling_ or put those duplicated identifiers into different _namespaces_ (like in C++). It would be better if `cgo` supported some mechanism to do automatic namespacing using Go package name, but until now (2020) you must do it yourself. The C preprocessor's "token pasting" operator `##` may help in this namespacing task. Eg. //File: my\_package1.h #define NS(id) my\_package1\_ ## id void NS(my\_function1)(int); void NS(my\_function2)(float); char NS(my\_shared\_var);https://stackoverflow.com/questions/56318343
复制相似问题