opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

MacOS Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies.

Open crow-ua opened this issue 3 years ago • 8 comments

MacOS Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies.

Environment

macOS Big Sur 11.6.1

<PackageReference Include="OpenCvSharp4" Version="4.5.5.20211231" />
<PackageReference Include="OpenCvSharp4.runtime.osx.10.15-x64" Version="4.5.5.20211231" />

What did you do when you faced the problem?

  1. Copied libOpenCvSharpExtern.dylib from runtimes/osx-x64/native to bin/debug/net5.0.
  2. Installed required dependencies manually:
brew install wget pkg-config mono-libgdiplus gtk+ ffmpeg glog yasm harfbuzz jpeg libpng libtiff openjpeg metis openblas opencore-amr protobuf tbb webp

Output:

Warning: wget 1.21.2 is already installed and up-to-date.
To reinstall 1.21.2, run:
  brew reinstall wget
Warning: pkg-config 0.29.2_3 is already installed and up-to-date.
To reinstall 0.29.2_3, run:
  brew reinstall pkg-config
Warning: mono-libgdiplus 6.1 is already installed and up-to-date.
To reinstall 6.1, run:
  brew reinstall mono-libgdiplus
Warning: gtk+ 2.24.33 is already installed and up-to-date.
To reinstall 2.24.33, run:
  brew reinstall gtk+
Warning: ffmpeg 4.4.1_5 is already installed and up-to-date.
To reinstall 4.4.1_5, run:
  brew reinstall ffmpeg
Warning: glog 0.5.0 is already installed and up-to-date.
To reinstall 0.5.0, run:
  brew reinstall glog
Warning: yasm 1.3.0_2 is already installed and up-to-date.
To reinstall 1.3.0_2, run:
  brew reinstall yasm
Warning: harfbuzz 3.2.0 is already installed and up-to-date.
To reinstall 3.2.0, run:
  brew reinstall harfbuzz
Warning: jpeg 9e is already installed and up-to-date.
To reinstall 9e, run:
  brew reinstall jpeg
Warning: libpng 1.6.37 is already installed and up-to-date.
To reinstall 1.6.37, run:
  brew reinstall libpng
Warning: libtiff 4.3.0 is already installed and up-to-date.
To reinstall 4.3.0, run:
  brew reinstall libtiff
Warning: openjpeg 2.4.0 is already installed and up-to-date.
To reinstall 2.4.0, run:
  brew reinstall openjpeg
Warning: metis 5.1.0 is already installed and up-to-date.
To reinstall 5.1.0, run:
  brew reinstall metis
Warning: openblas 0.3.19 is already installed and up-to-date.
To reinstall 0.3.19, run:
  brew reinstall openblas
Warning: opencore-amr 0.1.5 is already installed and up-to-date.
To reinstall 0.1.5, run:
  brew reinstall opencore-amr
Warning: protobuf 3.19.3 is already installed and up-to-date.
To reinstall 3.19.3, run:
  brew reinstall protobuf
Warning: tbb 2021.5.0 is already installed and up-to-date.
To reinstall 2021.5.0, run:
  brew reinstall tbb
Warning: webp 1.2.2 is already installed and up-to-date.
To reinstall 1.2.2, run:
  brew reinstall webp
  1. Checked dependencies for libOpenCvSharpExtern.dylib, everything is available.
otool -L libOpenCvSharpExtern.dylib

Output

libOpenCvSharpExtern.dylib:
	@rpath/libOpenCvSharpExtern.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/local/opt/freetype/lib/libfreetype.6.dylib (compatibility version 25.0.0, current version 25.1.0)
	/usr/local/opt/harfbuzz/lib/libharfbuzz.0.dylib (compatibility version 30200.0.0, current version 30200.0.0)
	/usr/local/opt/tesseract/lib/libtesseract.4.dylib (compatibility version 5.0.0, current version 5.1.0)
	/usr/lib/libcurl.4.dylib (compatibility version 7.0.0, current version 9.0.0)
	/usr/local/opt/ffmpeg/lib/libavcodec.58.dylib (compatibility version 58.0.0, current version 58.134.100)
	/usr/local/opt/ffmpeg/lib/libavformat.58.dylib (compatibility version 58.0.0, current version 58.76.100)
	/usr/local/opt/ffmpeg/lib/libavutil.56.dylib (compatibility version 56.0.0, current version 56.70.100)
	/usr/local/opt/ffmpeg/lib/libswscale.5.dylib (compatibility version 5.0.0, current version 5.9.100)
	/usr/local/opt/ffmpeg/lib/libavresample.4.dylib (compatibility version 4.0.0, current version 4.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 23.0.0)
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
	/System/Library/Frameworks/AVFoundation.framework/Versions/A/AVFoundation (compatibility version 1.0.0, current version 2.0.0)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1463.2.1)
	/System/Library/Frameworks/CoreMedia.framework/Versions/A/CoreMedia (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
	/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore (compatibility version 1.2.0, current version 1.11.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 2022.20.117)
	/System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 904.4.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.255.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1770.255.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
  1. Ensured project is running under 64 bit architecture:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
  1. Installed opencv via Homebrew.
  2. Looked into other closed issues, did not find solution to my problem.

Example code:

Console.WriteLine(Cv2.GetVersionString());

Output:

System.TypeInitializationException: The type initializer for 'OpenCvSharp.Internal.NativeMethods' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libOpenCvSharpExtern, 1): image not found
   at OpenCvSharp.Internal.NativeMethods.redirectError(CvErrorCallback errCallback, IntPtr userdata, IntPtr& prevUserdata)
   at OpenCvSharp.Internal.ExceptionHandler.RegisterExceptionCallback()
   at OpenCvSharp.Internal.NativeMethods.LoadLibraries(IEnumerable`1 additionalPaths)
   at OpenCvSharp.Internal.NativeMethods..cctor()
   --- End of inner exception stack trace ---
   at OpenCvSharp.Internal.NativeMethods.core_getVersionString(Byte* buf, Int32 maxLength)
   at OpenCvSharp.Cv2.GetVersionString()

What did you intend to be?

Library works and outputs version correctly.

crow-ua avatar Jan 25 '22 17:01 crow-ua

@shimat any ideas what else can be done in order to troubleshoot/fix this issue?

crow-ua avatar Feb 12 '22 18:02 crow-ua

Hello!

You have to use version 4 of tesseract and ffmpeg.

Currently brew installs version 5 which is not compatible with opencv.

Cheers!

obie73 avatar Mar 05 '22 09:03 obie73

I have the same problem

mimimiaomiao1 avatar May 21 '22 16:05 mimimiaomiao1

I have the same problem

mimimiaomiao1 avatar May 21 '22 16:05 mimimiaomiao1

As @obie73 said, the OpenCvSharp of version 4.5.5.2021.1213 is not compatible with the installed ffmpeg version 5.x.x. It has to install ffmpeg with version 4.x.x.

There is a cue from loading libOpenCvSharpExtern.dylib test.

❯ ./dyld_open_test /Users/siyao/Documents/astri-workspace/ART340/AstriEndoFormat/cli/bin/Debug/net6.0/runtimes/osx-x64/native/libOpenCvSharpExtern.dylib
dlopen(/Users/siyao/Documents/astri-workspace/ART340/AstriEndoFormat/cli/bin/Debug/net6.0/runtimes/osx-x64/native/libOpenCvSharpExtern.dylib, 
1): Library not loaded: /usr/local/opt/ffmpeg/lib/libavcodec.58.dylib
  Referenced from: /Users/siyao/Documents/astri-workspace/ART340/AstriEndoFormat/cli/bin/Debug/net6.0/runtimes/osx-x64/native/libOpenCvSharpExtern.dylib
  Reason: image not found
dlopen sample%          
                                                                                                                                                
❯ ll /usr/local/opt/ffmpeg/lib/
total 85168
-rw-r--r--   1 siyao  admin    11M Jul  6 17:08 libavcodec.59.18.100.dylib
lrwxr-xr-x   1 siyao  admin    26B Apr  4 22:40 libavcodec.59.dylib -> libavcodec.59.18.100.dylib

After brew install ffmpeg@4, it works out. (Remember link such as ln -s /usr/local/Cellar/ffmpeg@4/4.4.2_3/ /usr/local/opt/ffmpeg)

❯ ll /usr/local/Cellar/ffmpeg@4/4.4.2_3/lib
total 84720
-rw-r--r--   1 siyao  admin    11M Jul  6 17:13 libavcodec.58.134.100.dylib
lrwxr-xr-x   1 siyao  admin    27B Apr 15 04:13 libavcodec.58.dylib -> libavcodec.58.134.100.dylib

liviaerxin avatar Jul 06 '22 09:07 liviaerxin

I'm kinda curious what @rpath/libOpenCvSharpExtern.dylib (compatibility version 0.0.0, current version 0.0.0) means

gx56q avatar Nov 23 '22 20:11 gx56q

I was getting this problem and one of the errors referred to: could not find /usr/local/opt/tesseract/lib/tesseract.5.dylib I did a homebrew install of tesseract brew install tesseract and now it works. Only working on a AMD64 (x64) Mac and tested on Monterey and building within "Visual Studio for Mac"

<PackageReference Include="OpenCvSharp4" Version="4.7.0.20230115" />
<PackageReference Include="OpenCvSharp4.runtime.osx.10.15-x64" Version="4.6.0.20230105" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.7.0.20230115" />

FYI: https://www.oreilly.com/library/view/building-computer-vision/9781838644673/95de5b35-436b-4668-8ca2-44970a6e2924.xhtml

reo-1963 avatar Jun 01 '23 15:06 reo-1963

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 15 '23 06:12 stale[bot]