Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable
在ubuntu(18.04)运行OpenCvSharp4(4.6.0)报错如下:
Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMJAMSQJ3RO8", Request id "0HMJAMSQJ3RO8:00000005": An unhandled exception was thrown by the application.
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 LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory
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()
......
在程序根目录shell执行`ldd libOpenCvSharpExtern.so`,查看是否有存在`libOpenCvSharpExtern.so`文件,若显示各种依赖则存在。若不存在,需要copy到程序根目录。
执行`ldd libOpenCvSharpExtern.so`后如下:
root@filescan-5d79f649fc-xqrt9:/app# ldd libOpenCvSharpExtern.so
linux-vdso.so.1 (0x00007ffda5b3b000)
libtesseract.so.4 => not found
libgtk-x11-2.0.so.0 => not found
libgdk-x11-2.0.so.0 => not found
libcairo.so.2 => not found
libgdk_pixbuf-2.0.so.0 => not found
libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007fccca620000)
libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fccca309000)
libdc1394.so.22 => not found
libavcodec.so.57 => not found
libavformat.so.57 => not found
libavutil.so.55 => not found
libswscale.so.4 => not found
libjpeg.so.8 => not found
libpng16.so.16 => /usr/lib/x86_64-linux-gnu/libpng16.so.16 (0x00007fccca0d7000)
libtiff.so.5 => not found
libIlmImf-2_2.so.22 => not found
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fccc9eba000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fccc9cb6000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fccc9a97000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fccc988f000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fccc9506000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fccc9168000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fccc8f50000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fccc8b5f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fccce7d5000)
libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007fccc8957000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fccc86e6000)
可以看出有部分依赖的显示`not found`,需要额外去安装,知道全部依赖都查找得到既解决。
方法:
1、在docker hub找别人配置好环境的镜像(可能存在少量未找到,自行安装既可)
2、可参考如下
RUN apt-get update && apt-get install -y \
apt-utils \
libgdiplus \
libc6-dev \
libgtk2.0-dev \
libtbb-dev \
libatlas-base-dev \
libvorbis-dev \
libxvidcore-dev \
libopencore-amrnb-dev \
libopencore-amrwb-dev \
libavresample-dev \
x264 \
v4l-utils \
libwebp-dev \
tesseract-ocr \
libtesseract-dev \
libleptonica-dev \
libtiff-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libdc1394-22-dev \
libxine2-dev \
libv4l-dev \
tesseract-ocr
我最终的环境配置是:
FROM husty530/opencvsharp-ubuntu-1804:latest
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
RUN apt update
RUN apt install libgtk2.0-dev tesseract-ocr -y
RUN wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN rm packages-microsoft-prod.deb
RUN apt-get update
RUN apt-get install -y apt-transport-https
RUN apt-get update
RUN apt-get install -y aspnetcore-runtime-6.0