douli7841 2019-05-27 00:21
浏览 222

Golang:CGO移植包的多种定义

I have 2 project, the first, name as A, there is a submodule a imported sqlite3(github.com/mattn/go-sqlite3). Another B project import A's submodule a, and in another submodule b, it also import the same sqlite3.

Both A and B put there imports under vendor dir(managed by govendor). My Golang version is go version go1.12 linux/amd64.

While build B (go build main.go), throwing following errors(too many, part of them):

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

But building A works well. To testing the error, I started following demo, also with vendor inited by govendor, and build 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
 }

I think the compiler first compile A's sqlite3, objects are created under /tmp/go-link-281256755/000005.o (but no this dir after building), then compile B's importing of sqlite3 and also create a object contains the same-name function, then the compiler find 2 same-name symbols, the linking failed.

How to fix these situation? Is there any golang env settings to avoid these?


After I remove the sqlite3 package under vendor both of A and B, they both use the sqlite3 under ~/go/src/github.com/mattn/go-sqlite3/, they all build ok. But I can't do these, due to project A's deploy platform, I must put all dependencies under vendor, is there any another option to use multiple import with the same package?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助