Dart从2.5提供了FFI来支持外部函数调用,但是还是属于Dev阶段,总是会碰到一些奇怪的问题。

复现步骤

  1. https://github.com/dart-lang/samples clone到本地
  2. cd ffi/hello_word/hello_library
  3. cmake . && make
  4. 运行hello.dart
  5. 得到以下错误
Invalid argument(s): Failed to load dynamic library (dlopen(./hello_library/libhello.dylib, 1): no suitable image found.  Did find:
	/.../samples/ffi/hello_world/././hello_library/libhello.dylib: code signature in (/.../samples/ffi/hello_world/././hello_library/libhello.dylib) not valid for use in process using Library Validation: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.

解决

  1. 找到dart sdk路径,如果安装了通过flutter安装的,路径是[flutter sdk path]/bin/cache/dart-sdk/bin
  2. 备份dart可执行文件
  3. 运行codesign --remove-signature dart
  4. 运行codesign -dv dart检查是否得到以下结果dart: code object is not signed at all
    • 如果是,执行第5步
    • 如果不是,回到第3步
  5. 重新运行hello.dart

原因

因为dart可执行文件带签名,生成的动态库不带签名,两边对不上读取失败了。

BTW

如果有什么见解或者疑惑,欢迎留言!

参考

Github Issue