doufendi9063 2019-06-11 06:41
浏览 509

当我导入两个通过Cgo使用C程序包的不同程序包时,Go编译会返回x86_64体系结构错误的重复符号

Here is my code:

package main

import (
    kusb "github.com/karalabe/usb"
    tusb "github.com/trezor/trezord-go/usb"
)

func main() {
    kusb.Enumerate(0, 0)
    tusb.InitHIDAPI(nil)
}

When I compile (I'm using go mod to manage the packages), it returns this error:

duplicate symbol _libusb_dev_mem_alloc in:
    /var/folders/fm/1rln65d94mn45s0h5l78tdyh0000gp/T/go-link-624554542/000002.o
    /var/folders/fm/1rln65d94mn45s0h5l78tdyh0000gp/T/go-link-624554542/000020.o
ld: 136 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why?

Some investigation i had:

  1. The both packages use the same hidapi and libusb C packages in order to interact with usb devices.
  2. Those C packages are identical, hence it defines the same functions so i think it is directly related to the error.
  3. in trezord-go/usb, they include .C file, not the header file.

It is very counterintuitive to me because in the perspective of package users, I shouldn't need to worry about how a C package is used in the internal of the package, only the exposed types, functions and its behaviors.

Can anyone really explain what is going on here and how can I import both of them? They do different functions, eventhough they use the same C package.

  • 写回答

1条回答 默认 最新

  • duanquan1243 2019-06-13 15:55
    关注

    From here: https://www.repustate.com/blog/go-duplicate-symbols-for-architecture-x86_64/

    "What does this mean? Well, it means we're trying to link the same symbol name (in our case, a method) from two (or more) different source files. The fix was easy: rename one of the methods by updating the header file, the source file (.c or .cpp file) and lastly, updating your references to the symbol in your Go code, if it is directly referenced there."

    Will it help ?

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题