Linux编译opencv4.4.0报错“/usr/bin/ld: cannot find -lopencv_shape”
opencv4.4.0下载地址:https://sourceforge.net/projects/opencvlibrary/files/
问题描述
安装完opencv4.4.0后编译C++代码显示找不到lib库文件
/usr/bin/ld: cannot find -lopencv_shape
/usr/bin/ld: cannot find -lopencv_stitching
/usr/bin/ld: cannot find -lopencv_objdetect
/usr/bin/ld: cannot find -lopencv_superres
/usr/bin/ld: cannot find -lopencv_videostab
/usr/bin/ld: cannot find -lopencv_calib3d
/usr/bin/ld: cannot find -lopencv_features2d
/usr/bin/ld: cannot find -lopencv_highgui
/usr/bin/ld: cannot find -lopencv_videoio
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_video
/usr/bin/ld: cannot find -lopencv_photo
/usr/bin/ld: cannot find -lopencv_ml
/usr/bin/ld: cannot find -lopencv_imgproc
/usr/bin/ld: cannot find -lopencv_flann
/usr/bin/ld: cannot find -lopencv_core
collect2: error: ld returned 1 exit status
查看后台运行程序
ps
一、安装opencv依赖库
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev-is-python2 python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libjasper-dev(libjasper-dev依赖包可能找不到,参考博客)
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install ffmpeg
二、opencv编译安装
2.1 sudo cmake-gui
设置路径如下,点击configure,编译器使用默认即可
之后,修改两处地方,再点击generate
- 勾选上BUILD_opencv_world
- 在CMAKE_BUILD_TYPE后填上:RELEASE
如果有需要用到OpenCV contrib组件的,需要在OPENCV_EXTRA_MODULES_PATH
后填上opencv-contrib/modules
的路径
2.2编译
进入build文件夹,可以发现Makefile文件,输入sudo make开始编译
2.3安装
输入指令 sudo make install
2.4路径配置
sudo gedit /etc/ld.so.conf.d/opencv.conf
打开文件输入/usr/local/lib
执行生效指令sudo ldconfig
编辑sudo gedit /etc/bash.bashrc
输入export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
使环境变量生效source /etc/bash.bashrc
opencv4.4.0到此安装完成,但由于opencv4.4.0默认不生成opencv.pc,所以此时查看opencv版本会报错
查看opencv版本 指令:pkg-config opencv --modversion
解决方法:
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo gedit opencv.pc
输入
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.4.0
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
~
可能遇到的问题
用上文安装方法一直很顺利,可在测试程序时,还是出了点问题,如下,显示找不到lib库
这种问题的原因我也不太清楚,但我感觉应该是因为opencv4.4.0编译生成opencv4而不叫opencv,所以导致找不到
解决方法:
进入build文件夹,重新编译了一遍opencv,指令如下
sudo make uninstall
sudo cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo make
sudo make install
注:opencv4默认不生成opencv4.pc,指令-DOPENCV_GENERATE_PKGCONFIG=ON
可以生成opencv4.pc
把上文在/usr/local/lib/pkgconfig
底下建的opencv.pc先删除,输入pkg-config opencv4 --modversion
,可以看到版本信息,而输入pkg-config opencv --modversion
却报错,这时候编译测试代码,却发现找不到opencv
因此,我在底下复制了一份opencv4.pc,改名为opencv.pc,此时运行代码就不报错了。
所以我感觉还是opencv的名称有问题,如果按上文方法安装完成后,直接在/usr/local/lib/pkgconfig
底下建一个opencv4.pc应该就不报错的(我猜的,没试过)
附:测试代码
sudo g++ -std=c++11 `pkg-config opencv --cflags` opencvtest.cpp -o opencv `pkg-config opencv --libs`
opencvtest.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage = imread("lena.jpg");
imshow("源图像",srcImage);
waitKey(0);
return 0;
}
参考博客
在Linux下用正确的姿势安装OpenCV
Package opencv was not found in the pkg-config search path
更多推荐
所有评论(0)