'mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64') in Flutter
Executing dart run ffigen got the following error:
Unhandled exception:
Invalid argument(s): Failed to load dynamic library '/Users/bruce/homebrew/opt/llvm/lib/libclang.dylib': dlopen(/Users/bruce/homebrew/opt/llvm/lib/libclang.dylib, 0x0001): tried: '/Users/bruce/homebrew/opt/llvm/lib/libclang.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/Users/bruce/homebrew/Cellar/llvm/13.0.1_1/lib/libclang.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
#0 _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:12:43)
#1 new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:23:12)
#2 initializeGlobals (package:ffigen/src/header_parser/data.dart:41:33)
#3 initParser (package:ffigen/src/header_parser/parser.dart:48:3)
#4 parse (package:ffigen/src/header_parser/parser.dart:22:3)
#5 main (package:ffigen/src/executables/ffigen.dart:54:19)
#6 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#7 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
I checked my Flutter environment:
❯ file /Users/bruce/dev_tools/flutter/bin/cache/dart-sdk/bin/dart
/Users/bruce/dev_tools/flutter/bin/cache/dart-sdk/bin/dart: Mach-O 64-bit executable x86_64
When I run brew install llvm to install llvm, what I get is a library for arm64, which I think is causing the error:
❯ lipo -info /Users/bruce/homebrew/Cellar/llvm/13.0.1_1/lib/libclang.dylib
Non-fat file: /Users/bruce/homebrew/Cellar/llvm/13.0.1_1/lib/libclang.dylib is architecture: arm64
It seems that the Flutter SDK does not have a version for Apple Silicon, so how to solve this problem?
❯ flutter doctor -v
[✓] Flutter (Channel stable, 2.8.1, on macOS 12.3 21E230 darwin-arm, locale
zh-Hans-CN)
• Flutter version 2.8.1 at /Users/bruce/dev_tools/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 77d935af4d (4 months ago), 2021-12-16 08:37:33 -0800
• Engine revision 890a5fca2e
• Dart version 2.15.1
• Pub download mirror http://10.141.64.130:8080
[✓] Android toolchain - develop for Android devices (Android SDK version
32.1.0-rc1)
• Android SDK at /Users/bruce/Library/Android/sdk
• Platform android-32, build-tools 32.1.0-rc1
• Java binary at: /Applications/Android
Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
I tried installing the Dart SDK for the Arm platform, but it still didn't work, using the standalone Dart environment, I get the following error:
❯ /Users/bruce/dev_tools/dart/bin/dart run ffigen
The Flutter SDK is not available.
#0 BoundSdkSource.getDirectory (package:pub/src/source/sdk.dart:125:7)
#1 SystemCache.load (package:pub/src/system_cache.dart:113:52)
#2 Entrypoint._assertLockFileUpToDate (package:pub/src/entrypoint.dart:599:19)
#3 Entrypoint.assertUpToDate (package:pub/src/entrypoint.dart:525:7)
#4 getExecutableForCommand (package:pub/src/executable.dart:304:16)
#5 RunCommand.run (package:dartdev/src/commands/run.dart:232:32)
#6 CommandRunner.runCommand (package:args/command_runner.dart:209:27)
#7 DartdevRunner.runCommand (package:dartdev/dartdev.dart:232:30)
#8 CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:119:25)
#9 new Future.sync (dart:async/future.dart:296:31)
#10 CommandRunner.run (package:args/command_runner.dart:119:14)
#11 runDartdev (package:dartdev/dartdev.dart:67:29)
#12 main (file:///opt/s/w/ir/cache/builder/sdk/pkg/dartdev/bin/dartdev.dart:11:9)
#13 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:293:32)
#14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
Solution 1:[1]
I had passed thought this and it's awful! The problem here is how you installed LLVM, the LLVM itself installed via Home-brew by default as ARM64, but you should had installed the X86_64 version of it. To solve try this:
- Uninstall current LLVM:
brew uninstall llvmss - Make sure to have a x86_64 of homebrew installed as showed in this medium article
- Then install with homebrew x86 version:
arch -x86_64 /usr/local/homebrew/bin/brew install llvm
Then you ffi gen will work.
Solution 2:[2]
Thanks to Richard Heap's suggestion, I've solved the problem!
As a Flutter project, you can't execute the dart command directly to the project directory, because some cache already exists after executing flutter pub get. Clear the package cache first, then execute dart pub get, and it works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sávio Batista |
| Solution 2 | arcticfox |
