11

C++使用opencv-3.4.0自带dnn库时编译时报错:undefined reference to `cv::dnn::exper...

 3 years ago
source link: https://blog.popkx.com/undefined-reference-to-cv-dnn-experimental_dnn_v3-Net-Net/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++使用opencv-3.4.0自带dnn库时编译时报错:undefined reference to `cv::dnn::experimental_dnn_v3::Net::Net

发表于 2019-12-17 17:12:03   |   已被 访问: 1,976 次   |   分类于:   杂谈 , 深度学习   |   暂无评论

使用 opencv-3.4.0 编写C++语言图像处理程序时,使用到了 caffe 模型,因此写出了下面这样的代码:

cv::dnn::Net net_ = cv::dnn::readNetFromCaffe(prototxt, caffemodel);
...

然后编译之,发现居然报错了:

undefined reference to `cv::dnn::experimental_dnn_v3::Net::Net

看错误信息应该是提示找不到“Net”函数,opencv-3.4.0 是支持 Net 的,因此这是一个很奇怪的问题。

仔细观察错误信息,发现提示的是cv::dnn::experimental_dnn_v3::Net::Net,而我代码中写的是cv::dnn::Net,这里有些差异。再查看我的编译命令:

$ g++ *.cpp -o test.out -L../opencv-3.4.0/lib`pkg-config --cflags --libs opencv`

可能是链接的库出错了,单独执行pkg-config

$ pkg-config --cflags --libs opencv
-I/usr/include/opencv ...

看到输出-I/usr/include/opencv就明白了:虽然我显式的指定了 opencv-3.4.0 的路径,但是pkg-config并不会从该路径查找相应的库,它仅从环境变量PKG_CONFIG_PATH中查找,因为我没有指定该环境变量,所以它自然从系统库查找了。

知道原因,解决问题就容易了,有两个方法:

  1. 不使用pkg-config命令,手动的一个个链接库 -lopencv_video -lopencv_photo -lopencv_imgproc ...
  2. 指定环境变量export PKG_CONFIG_PATH=<opencv-3.4.0绝对路径>/lib/pkgconfig,然后再编译

阅读更多:   杂谈 , 深度学习


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK