Category Archives: Coding

Build Debug Environment of OpenCV 3.1 C++ with Eclipse in Ubuntu

To build the debug environment, the first part is to set up OpenCV.

  1. Download the latest version of OpenCV 3.x from
    http://sourceforge.net/projects/opencvlibrary
    Then unzip it to certain folder, say /home/tc/Documents
  2. Update and upgrade Ubuntu for the preparation installation, and required packages
    • sudo apt-get update
    • sudo apt-get upgrade
    • sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev
  3. In terminal, browse to the unzipped folder, make a new release folder and generate the Makefile from cmake. Then make install.
    • cd /opencv-x.x.x
    • mkdir release
    • cd release
    • cmake -DWITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON ..
    • sudo make
    • sudo make install
  4. If all is well without any error message, continue to step 5. Otherwise, install required package and start from cmake again.
  5. Edit OpenCV configuration file with appending the following line to the opencv.conf file:
    • sudo gedit /etc/ld.so.conf.d/opencv.conf
    • add   /usr/local/lib
  6. Then add the pkgconfig OpenCV path to ./bashrc file for final environment setup
    • sudo ldconfig
    • sudo gedit /etc/bash.bashrc
    • add  PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    • add  export PKG_CONFIG_PATH

After this, the OpenCV is successfully set up in Ubuntu. The next part is go for Eclipse C++ installation with library include configuration.

  1. Install latest Eclipse with cdt and C++ package on ubuntu
    • sudo apt-get install eclipse eclipse-cdt g++
  2. Open Eclipse, and start a new C++ Project by choosing ‘Empty Project’ with Toolchains ‘Cross GCC’. Fill a name you like, say ‘testopenCV’. Create a new C++ flie with ‘cpp’ extension, say ‘src.cpp’.
  3. Add external OpenCV properties to the project. To find the location of OpenCV and its libraries, type:
    • pkg-config  – -cflags opencv
    • pkg-config  – -libs opencv
  4. Normally, the OpenCV installation folder is ‘/usr/local/include/opencv’ and its library folder is ‘/usr/local/lib’. To add them in the properties, open the ‘Properties’ of the project and go to ‘C/C++ Build’, then ‘Setting’. In ‘Cross GCC Compiler’ –> ‘Includes’ –> ‘Include paths(l)’ add OpenCV installation folder (/usr/local/include/opencv).
  5. Go to ‘Cross G++ Compiler’ –> ‘Includes’ –> ‘Include paths’, add the same address (/usr/local/include/opencv).
  6. The for library, go to ‘Cross G++ Linker’ –> ‘Libraries’ –> ‘Library search path (-L)’, add library path (/usr/local/lib). Then in ‘Libraries (-l)’, add libraries names for applying in the search. Normally except the core, all others are optional for specific programs you implement. If other libraries are needed, just add them in.
    • opencv_core   opencv_imgproc   opencv_legacy   opencv_contrib   opencv_highgui   opencv_features2d   opencv_flann   opencv_video   opencv_objdetect   opencv_ml   opencv_calib3d
  7. That’s it for the new C/C++ project with OpenCV implementation. If you want to import codes from others and link to OpenCV:
    • Go to ‘C/C++ General’ from project properties, then click ‘Paths and Symbols’ –> ‘Includes’
    • In both ‘GNU’ and ‘GNU C++’, add the same include folder in the ‘Include directories’, say ‘/usr/local/include/opencv’.
    • In the tab ‘Libraries’, add all libraries needed one by one, similar in step 6.
  8. Restart Eclipse and enjoy!

g++ hello.cpp -o hello`pkg-config –cflags –libs opencv`

Reference: http://www.installthetech.com/blogs/2016/2/28/linking-eclipse-cdt-with-opencv-249-in-ubuntu-1404