question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
liviaerxincommented, Nov 25, 2022

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

0reactions
reo-1963commented, Jun 1, 2023

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

MacOS runtime error (Unable to load shared library ' ...
"Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting ...
Read more >
Unable to load shared library 'OpenCvSharpExtern' VS for ...
In short: the only workaround I've found is to build OpenCV and OpenCVSharpExtern right inside your container. Works quite well.
Read more >
'OpenCvSharp.NativeMethods' threw an exception. Unable ...
'OpenCvSharp.NativeMethods' threw an exception. Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies., Ubuntu 18.04.
Read more >
MacOS runtime error (Unable to load shared library ' ...
I get Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies constantly while trying to create Mat object.
Read more >
opencvsharp
Native binding (OpenCvSharpExtern.dll / libOpenCvSharpExtern.so) is required to work OpenCvSharp. To use OpenCvSharp, you should add both OpenCvSharp4 and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found