Linux多版本opencv配置与“cannot open shared object file: No such file or directory”问题解决

原装opencv2.4.13,sudo make install之后安装到/usr/local/下。

现装opencv3.4.1,为了避免冲突,重新指定安装目录。

1.首先在OpenCV主页下载源码

2.新建文件夹(mydir)用于安装opencv3.4.1

3.修改源码解压之后文件夹中的CMakeLists.txt中安装路径为:

set(CMAKE_INSTALL_PREFIX "mydir" CACHE PATH "Installation Directory")

注意对应Linux平台。然后cmake,make,sudo make install即可安装opencv3.4.1到mydir。(make -j4)表示启用4个线程编译,加快速度。至此安装成功。

4.在opencv编译好后,所在目录中一般会有一个叫OpenCVConfig.cmake的文件,这个文件中指定了CMake要去哪里找OpenCV,其.h文件在哪里等。你会发现这一行set(OpenCV_INCLUDE_DIRS     ......    )都变成了/mydir/.....

5.要让CMake找到OpenCVConfig.cmake文件,就指定了opencv的路径。在你自己工程里的CMakeLists.txt里面加上set(OpenCV_DIR "/mydir/opencv-3.4.1/build"),注意加在project()之前;如下:

cmake_minimum_required(VERSION 2.8)
set(OpenCV_DIR "/mydir/build")
PROJECT (HELLO)
SET(SRC_LIST main.c)
find_package(OpenCV REQUIRED)

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

cmake之后出现如下信息表示成功。

Linux多版本opencv配置与“cannot open shared object file: No such file or directory”问题解决_第1张图片

6.在有些IDE(如Eclipse CDT)中添加动态库之后编译成功,但运行时可能会出现

error while loading shared libraries: libopencv_core.so.2.4: cannot open shared object file: No such file or directory

这是因为编译器自己找不到安装后的opencv库的路径,所以需要告诉系统到指定的地方链接。sudo 编辑

/etc/ld.so.conf.d/opencv.conf

加上一行

/mydir/lib

mydir是前文自己安装opencv3.4.1的目录。然后运行

sudo ldconfig -v

即可解决。

7.参考:

https://stackoverflow.com/questions/12335848/opencv-program-compile-error-libopencv-core-so-2-4-cannot-open-shared-object-f

https://blog.csdn.net/jay463261929/article/details/53860593

你可能感兴趣的:(OpenCV)