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.

Can't link `OpenCvSharpExtern` on Linux

See original GitHub issue

Summary of your issue

I can’t figure out how to get my program to link the OpenCvSharpExtern library at runtime on Linux.

Environment

A 64-bit Linux Mint (LMDE 2 Betsy) VM with .NET Core v1.1.2, OpenCV 3.2.0 and OpenCvSharp3-AnyCPU at v3.2.0.20170419.

Linux version 3.16.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.8.4 (Debian 4.8.4-1) ) #1 SMP Debian 3.16.43-2+deb8u2 (2017-06-26)

What did you do when you faced the problem?

I tried to compile and run my .NET Core project using OpenCVSharp, which runs fine on Windows, on a Linux machine. I performed the following steps (after setting up the .NET Core toolchain):

  • Download v3.2.0 snapshots of OpenCV and OpenCV_Contrib.
  • Compile and install OpenCV 3.2.0 with Contrib modules according to OpenCV tutorials:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules
sudo make install
  • Clone OpenCVSharp repository
  • Configure CMake to call compiler in C++11 mode (so it doesn’t trip on the non-spaced >>) CMAKE_CXX_FLAGS = -std=c++11
  • Build according to Unix tutorial
cmake .
make
  • Copy built libOpenCvSharpExtern.so to the working directory of my executable.
  • (For good measure, also add a copy of the file as OpenCvSharpExtern.dll as I’ve seen suggested elsewhere.)
  • Try to run my program.

Example code:

In project file:

<PackageReference Include="OpenCvSharp3-AnyCPU" Version="3.2.0.20170419"/>

In code:

using OpenCvSharp;

# Code in xUnit testcase:
Mat testImage = Cv2.ImRead("Input.jpg");

Run:

dotnet restore
dotnet build
dotnet test

Output:

It’s the well-known dynamic linking failure that has been giving many other people trouble before:

[xUnit.net 00:00:01.5142311]     Namespace.Testcase [FAIL]
[xUnit.net 00:00:01.5277401]       System.TypeInitializationException : The type initializer for 'OpenCvSharp.NativeMethods' threw an exception.
[xUnit.net 00:00:01.5295641]       ---- OpenCvSharp.OpenCvSharpException : Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found.
[xUnit.net 00:00:01.5308765]        (Exception from HRESULT: 0x8007007E)
[xUnit.net 00:00:01.5322714]       *** An exception has occurred because of P/Invoke. ***
[xUnit.net 00:00:01.5336770]       Please check the following:
[xUnit.net 00:00:01.5349880]       (1) OpenCV's DLL files exist in the same directory as the executable file.
[xUnit.net 00:00:01.5364421]       (2) Visual C++ Redistributable Package has been installed.
[xUnit.net 00:00:01.5378030]       (3) The target platform(x86/x64) of OpenCV's DLL files and OpenCvSharp is the same as your project's.
[xUnit.net 00:00:01.5394495]       
[xUnit.net 00:00:01.5410275]       System.DllNotFoundException: Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found.
[xUnit.net 00:00:01.5431416]        (Exception from HRESULT: 0x8007007E)
[xUnit.net 00:00:01.5446565]          at OpenCvSharp.NativeMethods.core_Mat_sizeof()
[xUnit.net 00:00:01.5459460]          at OpenCvSharp.NativeMethods.TryPInvoke()
[xUnit.net 00:00:01.5470083]       -------- System.DllNotFoundException : Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found.
[xUnit.net 00:00:01.5486279]        (Exception from HRESULT: 0x8007007E)
[xUnit.net 00:00:01.5502861]       Stack Trace:
[xUnit.net 00:00:01.5574166]            at OpenCvSharp.NativeMethods.imgcodecs_imread(String filename, Int32 flags)
[xUnit.net 00:00:01.5604129]            at OpenCvSharp.Mat..ctor(String fileName, ImreadModes flags)
[xUnit.net 00:00:01.5611202]            at OpenCvSharp.Cv2.ImRead(String fileName, ImreadModes flags)
[xUnit.net 00:00:01.5615574]         /home/username/Project/Filename.cs(33,0): at Namespace.Testcase()
[xUnit.net 00:00:01.5620643]         ----- Inner Stack Trace -----
[xUnit.net 00:00:01.5624050]            at OpenCvSharp.NativeMethods.TryPInvoke()
[xUnit.net 00:00:01.5627827]            at OpenCvSharp.NativeMethods..cctor()
[xUnit.net 00:00:01.5631150]         ----- Inner Stack Trace -----
[xUnit.net 00:00:01.5634355]            at OpenCvSharp.NativeMethods.core_Mat_sizeof()
[xUnit.net 00:00:01.5638368]            at OpenCvSharp.NativeMethods.TryPInvoke()
[xUnit.net 00:00:01.5685690]       Output:
[xUnit.net 00:00:01.5740603]         pwd: /home/username/Project/bin/Debug/netcoreapp1.1
[xUnit.net 00:00:01.6465541]   Finished:    Project

What did you intend to be?

Using the exact same codebase, the code should compile and run under Linux just the same as it does under Windows. The .DLL files packed with the NuGet release of OpenCvSharp3 seem to be Windows-only, but replacing them with a version compiled on the target Linux system from the OpenCvSharp codebase should result in a version that is possible to link and use.

Am I doing something wrong here, or missing a step?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
ArXen42commented, Apr 21, 2020

@vinayakcybage what worked for me on ArchLinux:

  1. Built libOpenCvSharpExtern.so on my pc (there is section on that in README.md, surprisingly it takes only 5 minutes).
  2. Renamed it to just libOpenCvSharpExtern (no extension) and copied to /usr/lib/.
  3. Attempted to run dotnet program. It will likely fail with similar message, but instead of In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory ---> there will be some other library name, which you should install using your package manager. Alternatively, you can use ldd to get missing dependencies.

Using binary provided by OpenCvSharp package didn’t work for me because it was compiled against specific versions of system libraries that are older than mine.

Currently trying to figure out how to make it work under docker.

0reactions
sb-aanandhcommented, May 23, 2022

I am still facing this issue. I have compiled “OpenCV 4.5” on Ubuntu 21.10 and “OpenCVSharp 4.5.5.” successfully. Whenever I run the dotnet run / dotnet., it opens with the same error , unable to load the libOpenCVSharpExtern . What is the best way to resolve this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run OpenCvSharp on Ubuntu 18.04?
I have a problem with running OpenCvShare on the Ubuntu Linux. It keeps saying that libOpenCvSharpExtern is not found. I have a problem...
Read more >
opencvsharp
OpenCvSharp won't work on Unity and Xamarin platform. For Unity, please consider using OpenCV for Unity or some other solutions. · OpenCvSharp does...
Read more >
Camera-Related Package that works well with C# in ...
Yes I have confirmed that the "OpenCvSharpExtern" does exist on my file but somehow it can't read it. Although this subreddit is not...
Read more >
ldd tells me my app is "not a dynamic executable"
I managed to get it running on CentOS a year ago, but now when I am setting up a new CentOS VM, it...
Read more >
OpenCVSharp for Unity | Page 2
I get this error message when I try to return a Mat for instance: Cannot convert from out 'OpenCvSharp.Mat[]' to 'OpenCvSharp.Point[][]' .
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