背景

参考官方文档: golang_setup · Tencent/MMKV Wiki (github.com)amd64 host 环境上交叉编译aarch64架构时出现以下的问题:

  1. 编译mmkv静态库时:提示selected processor does not support "aese v6.16b,v0.16..." 报错信息.

  2. cgo提示 cannot find -lz

  3. cgo链接报错:

# mmkv_test
/usr/local/go/pkg/tool/linux_amd64/link: running aarch64-linux-g++ failed: exit status 1
/tmp/go-link-1100075970/000002.o: In function `_cgo_eea72cd326f0_Cfunc_checkReSetCryptKey':
/tmp/go-build/cgo-gcc-prolog:146: undefined reference to `checkReSetCryptKey'
/tmp/go-link-1100075970/000002.o: In function `_cgo_eea72cd326f0_Cfunc_cryptKey':
/tmp/go-build/cgo-gcc-prolog:228: undefined reference to `cryptKey'
/tmp/go-link-1100075970/000002.o: In function `_cgo_eea72cd326f0_Cfunc_reKey':
/tmp/go-build/cgo-gcc-prolog:1004: undefined reference to `reKey'
collect2: error: ld returned 1 exit status

相关issue: https://github.com/Tencent/MMKV/issues/1212

解决方案:

问题 1: 如果不需要加密功能,直接开启MMKV_DISABLE_CRYPT宏,然后再按文档进行编译。

问题 2: 这是由于未找到aarch64版本的libz导致, 解决方式为:

  1. 静态编译zlib.
  2. libz.a文件放到第一个问题编译出来的mmkv静态库的lib目录中.
  3. 重新编译.

问题3: 这是由于开启MMKV_DISABLE_CRYPT时,没有兼容golang引发的问题,相关pr: https://github.com/Tencent/MMKV/pull/1213

整体编译过程:

  1. 下载aarch64 交叉编译链工具: https://toolchains.bootlin.com
  2. 下载zlib源码
     wget https://www.zlib.net/zlib-1.3.tar.gz
    
  3. 静态编译zlib源码
    ./configure --static --prefix=. #需要先设置CC,CXX等环境变量
    
  4. 复制zlib.alib目录
  5. go build