Golang中Gomobile bind无法找到CGO库的问题
Golang中Gomobile bind无法找到CGO库的问题 “gomobile” bind 命令似乎无法找到 CGO 库。 我已启用 CGO,但问题依旧存在。 如果有人能就此问题提供帮助,我将不胜感激。
您使用的是哪个版本的 Go (go version)?
$ go version
go version go1.14.1 darwin/amd64
此问题在最新版本中是否重现?
是
您使用的是哪种操作系统和处理器架构 (go env)?
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/younseo/Library/Caches/go-build"
GOENV="/Users/younseo/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/younseo/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/opt/go/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/yq/4rz_9mts3w725cccjrp66pxm0000gn/T/go-build490416867=/tmp/go-build -gno-record-gcc-switches -fno-common"
您做了什么?
$gomobile bind -target ios
$gomobile bind -target android
您期望看到什么?
期望生成 .arr、.jar、Framework 文件
您实际看到了什么?
gomobile bind 未能找到 cgo 库。

更多关于Golang中Gomobile bind无法找到CGO库的问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于Golang中Gomobile bind无法找到CGO库的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
这是一个常见的gomobile绑定问题,通常是由于CGO库路径配置不正确导致的。以下是解决方案:
首先,确保正确设置了CGO环境变量。对于iOS,需要指定C编译器:
// 对于iOS目标
export CGO_ENABLED=1
export GOOS=darwin
export GOARCH=arm64
export CC=$(go env GOROOT)/misc/ios/clangwrap.sh
对于Android,需要设置NDK路径:
// 对于Android目标
export CGO_ENABLED=1
export GOOS=android
export GOARCH=arm64
export CC=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang
如果使用gomobile init,确保已正确初始化:
gomobile init -ndk $ANDROID_NDK_HOME
检查gomobile绑定命令的完整语法:
// 绑定iOS框架
gomobile bind -target ios -o ./output/MyFramework.framework ./mypackage
// 绑定Android库
gomobile bind -target android -o ./output/mylibrary.aar ./mypackage
如果问题仍然存在,尝试清理并重新构建:
go clean -cache
gomobile clean
gomobile init
确保你的包结构正确,并且导出的函数使用了正确的注释:
package mypackage
import "C"
//export MyFunction
func MyFunction() int {
return 42
}
对于复杂的CGO依赖,可能需要指定额外的链接器标志:
gomobile bind -target ios -ldflags="-L/path/to/library" ./mypackage

