Dart ffi unhandled exception解决办法
目录
Dart从2.5提供了FFI来支持外部函数调用,但是还是属于Dev阶段,总是会碰到一些奇怪的问题。
1 复现步骤
- 从 https://github.com/dart-lang/samples clone到本地
cd ffi/hello_word/hello_library
cmake . && make
- 运行
hello.dart
- 得到以下错误
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.
2 解决
- 找到dart sdk路径,如果安装了通过flutter安装的,路径是
[flutter sdk path]/bin/cache/dart-sdk/bin
- 备份
dart
可执行文件 - 运行
codesign --remove-signature dart
- 运行
codesign -dv dart
检查是否得到以下结果dart: code object is not signed at all
- 如果是,执行第5步
- 如果不是,回到第3步
- 重新运行hello.dart
3 原因
因为dart可执行文件带签名,生成的动态库不带签名,两边对不上读取失败了。
4 BTW
如果有什么见解或者疑惑,欢迎留言!