mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64') in Flutter

128.8k Views Asked by At

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)
10

There are 10 best solutions below

0
On

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:

  1. Uninstall current LLVM: brew uninstall llvm ss
  2. Make sure to have a x86_64 of homebrew installed as showed in this medium article
  3. Then install with homebrew x86 version: arch -x86_64 /usr/local/homebrew/bin/brew install llvm

Then you ffi gen will work.

0
On

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.

0
On

ReInstall Cocoapods from Homebrew (https://formulae.brew.sh/formula/cocoapods#default) :

brew install cocoapods

You may need to reinstall Homebrew first

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

this is from their homepage

0
On

I encountered the same problem in VS Code, and I tried below instructions, then it worked.

  1. open the workspace setting in JSON format and add.
"terminal.integrated.profiles.osx": { "x86 bash": { "path": "/usr/bin/arch",
"args": [ "-arch", "x86_64", "/bin/bash" ] } },
"terminal.integrated.defaultProfile.osx": "x86 bash"

Here is the above code image.

code

  1. then open workspace setting in UI type defualt profile in search area than select workspace→terminal then select X86 bash

settings

1
On

I got this same import error with uamqp library

ImportError: dlopen(/Users/maheshwaran/Documents/newenv/lib/python3.10/site-packages/uamqp/c_uamqp.cpython-310-darwin.so, 0x0002): tried: '/Users/maheshwaran/Documents/newenv/lib/python3.10/site-packages/uamqp/c_uamqp.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))

I resolved this import error by running below command:

  1. Uninstalling uamqp library:

    pip uninstall uamqp

  2. Setting ARCHFLAGS for arm64 architecture and using no cache so it will download respective library suits arm64 architecture.

    ARCHFLAGS="-arch arm64" pip install uamqp --compile --no-cache-dir

Hope this helps you as well !!

0
On

I had the same problem. When I downloaded PyCharm, I didn't notice that the Mac version actually has two versions listed, the Intel version and the Apple Silicon version. The website defaulted to the Intel version (pycharm-professional-2022.3.2.dmg). I went back to the site and downloaded the Apple Silicon Version (pycharm-professional-2022.3.2-aarch64.dmg), remove the one I had installed and installed the new version. That fixed my problem. I hope this works for you.

0
On

If you see this issue with the Nokogiri gem in Rails, remove

nokogiri (1.13.6-arm64-darwin)
      racc (~> 1.4)

from your Gemfile.lock and do a bundle install.

1
On

This error appears on MacBooks with an Apple Silicon M1 chip. The problem appears only when running a command thru Terminal configured to be opened with Rosetta.

What I did as a workaround was to:

  1. Clone Terminal application
  2. Rename it to "Terminal x86_64"
  3. Got info by right click + "Get info"
  4. Unchecked "Open with Rosetta"

So, every time I want to run a command that causes this error, I use the "Terminal x86_64" to run it and guess what? It works.

5
On

Running on M1 mac, these two commands solved my problem.

$ arch -arm64 brew install llvm

$ sudo gem install ffi
1
On

Here is the steps that worked for me,

  1. Install llvm with homebrew x86_64 version arch -x86_64 /usr/local/homebrew/bin/brew install llvm

  2. Install ffi for x86-64 version sudo arch -x86_64 gem install ffi

  3. Delete the pods directory and Podfile in iOS directory

  4. Invalidate cache and restart android studio