cgo调用静态库失败(我的macos版本是:10.15.5)

elvindu · 2020-06-08 09:36:36 · 4622 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2020-06-08 09:36:36 的主题,其中的信息可能已经有所发展或是发生改变。

cgo调用静态库失败 我的macos版本是:10.15.5

golang版本是:1.14.4

编译golang程序时,链接静态库报以下错误:

# command-line-arguments

ld: warning: ignoring file ./libtest.a, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )

Undefined symbols for architecture x86_64:

  "_Add", referenced from:

      __cgo_cb1f4926c095_Cfunc_Add in _x002.o

     (maybe you meant: __cgo_cb1f4926c095_Cfunc_Add)

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

我用的链接方式如下:

package main
/*
#cgo LDFLAGS: -L ./ -ltest
#include "test.h"
 */
import "C"
import "fmt"

func main() {
   fmt.Println(C.Add(C.int(1),C.int(2)))

}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

4622 次点击  
加入收藏 微博
3 回复  |  直到 2021-03-23 14:03:47
elvindu
elvindu · #1 · 5年之前

找到原因了。是因为mac上面装了binutils,这个工具会改变ar,sanlib(建立静态库符号索引用的)的指向。指向了linux的工具,所以找不到symbol。

hijackwust
hijackwust · #2 · 4年之前

我也遇到相同问题,你是怎么处理的? 难道卸载掉 binutils?

elvindu
elvindu · #3 · 4年之前

@hijackwust 是的。

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传