cgo调用静态库失败
我的macos版本是:10.15.5
golang版本是:1.14.4
编译golang程序时,链接静态库报以下错误:
```shell
# 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)
```
我用的链接方式如下:
```go
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)))
}
```
更多评论
找到原因了。是因为mac上面装了binutils,这个工具会改变ar,sanlib(建立静态库符号索引用的)的指向。指向了linux的工具,所以找不到symbol。
#1