Package OpenCV not found? Let’s Find It.

转载地址:http://prateekvjoshi.com/2013/10/18/package-opencv-not-found-lets-find-it/

OpenCV has been refined over the years and installing it has become way more user-friendly now. If has been ported to many platforms and you don’t need an IDE to use it. There are many different ways in which you can install it and use it in your existing code. But sometimes, when people try to do different things with it, they run into problems. This post deals with one such problem. When you write code that uses OpenCV, you need to tell your compiler where it is. The reason for this is that this location will contains all the libraries and binaries necessary for the header files to work in your code. If you don’t specify this, you will encounter the following error:  

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

Where do we go from here?

The reason for the error is that when you try to compile your code from the command line, you need to tell it where to find OpenCV related stuff. People generally use pkg-config to take care of this because it’s easy to handle and it looks clean. Imagine doing the ‘-l’ library-includes every time you want to compile some code. Tedious! Just open your terminal and type the following:

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

This is the path to opencv.pc by default. If you have already installed OpenCV, you should already have opencv.pc somewhere on your machine. So in case you don’t have opencv.pc file in that path, just search for it and assign the path to the PKG_CONFIG_PATH variable:

$ export PKG_CONFIG_PATH=/path/to/the/file

And you are done! It’s better to add this line to your bashrc file so that you don’t have to do it every single time you reopen your terminal.

I can’t find my opencv.pc file, what do I do?

If you cannot find the opencv.pc file, don’t worry! Just create a new file called opencv.pc in /usr/local/lib/pkgconfig and add the following content to that file:

prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

After creating this file, make sure the PKG_CONFIG_PATH variable contains this path and you are good to go.

How do I run my OpenCV code now?

Now that all that installing stuff is out of the way, let’s see how we can run your OpenCV code:

  1. Install opencv on your system.
  2. Unzip the zip file
  3. Open the terminal and go to that folder
  4. Type the following on your terminal to compile:
    $ g++ main.cpp MyFile.h MyFile.cpp `pkg-config opencv --cflags --libs` -o main.o
  5. Type the following on your terminal to run (you may need to chmod 777 main.o):
    $ ./main.o

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值