MacOS 下 cgo 的库依赖问题的解决过程。

原创
08/25 12:17
阅读数 383

碰到了这个问题:

go get -v github.com/labstack/echo
runtime/cgo
# runtime/cgo
ld: library not found for -lpthread
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

先尝试粗暴的更新了 Xcode, 升级 BigSur 没有解决这个问题。 于是冷静下来分析,根据经验,应该是静态库/动态库的路径没有指定正确导致。 Mac 系统的库比较复杂,不像 Linux 那么直接,不管三七二十一,先查找一波:

find / -name "libpthread*"
find / -name "pthread*"

找到看起来有用的一些信息:

/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/pthread/pthread.h
/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/pthread.h
/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/share/man/man3/pthread.3
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/pthread/pthread.h
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/pthread.h
/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/share/man/man3/pthread.3
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/pthread/pthread.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/pthread.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/share/man/man3/pthread.3

这应该是开发工具依赖的一些头文件和库目录 /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk 了,于是,切进去看看,我们重点是找 lib:

cd /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib
ll libpthread*
lrwxr-xr-x  1 root  wheel    13B Jun 23 19:21 libpthread.tbd -> libSystem.tbd

这个 libpthread.tbd 看起来很可疑,查了一下是 Mac 下的特殊文本格式,里面记录了真正的链接库的信息。不管了,先指定目录吧:

export LD_LIBRARY_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib
export LIBRARY_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib

然后再来:

go get -v github.com/labstack/echo
runtime/cgo
net
net/textproto
vendor/golang.org/x/net/http/httpproxy
crypto/x509
vendor/golang.org/x/net/http/httpguts
crypto/tls
net/http/httptrace
net/http
golang.org/x/crypto/acme
golang.org/x/crypto/acme/autocert
github.com/labstack/echo

问题解决!

总结:

遇到问题不要慌,先发个朋友圈。

展开阅读全文
打赏
1
0 收藏
分享
加载中
修罗做事总是那么仔细
09/29 05:46
回复
举报
更多评论
打赏
1 评论
0 收藏
1
分享
返回顶部
顶部