Use OpenCV in Eclipse CDT

Install OpenCV (see previous posts for Ubuntu 10.10, Ubuntu 11.10, Ubuntu 12.04 and OpenCV 2.4 in Ubuntu 12.04).

Install Eclipse CDT:

sudo apt-get install eclipse-cdt

Open Eclipse CDT and select a workspace (it’s just a path to store projects together). Create a new “C++ Project”, give it a name and select “Empty Project” on “Project type” and “Linux GCC” on “Toolchains”.

Right click on the project folder and select “Properties”. Go to “C/C++ Build->Settings”. Then, inside “Tool Settings” tab, go to “GCC C++ Compiler->Includes” and fill “Include paths (-l)” with the path of your OpenCV include installation. If you installed OpenCV from the source code it will possibly be “/usr/local/include/opencv2” (or the path you entered in CMAKE_INSTALL_PREFIX when configuring with CMake), but if you installed from the repositories it should be “/usr/include/opencv2“.

Now go to “GCC C++ Linker->Libraries” and fill “Libraries (-l)” with (at least) those OpenCV libraries:

opencv_core
opencv_highgui

And “Library search paths (-L)” with the path location of your OpenCV installation libraries. As before, if you installed from the source it should be “/usr/local/lib” (same here with CMAKE_INSTALL_PREFIX) and if you installed with repositories “/usr/lib“.

Now it’s time to code. Go to “File->New->Source file” and create a new file called “main.cpp“. Fill it with:

#include "opencv.hpp"

int main(int argc, char* argv[])  {

        IplImage* img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 3 );

        cvCircle( img, cvPoint( 320, 240 ), 100, cvScalar( 255, 0, 0 ), 5 );

        cvNamedWindow( "OpenCV Window", CV_WINDOW_NORMAL );
        cvShowImage( "OpenCV Window", img );

        cvWaitKey(0);

        cvDestroyWindow( "OpenCV Window" );
        cvReleaseImage( &img );

        return 0;
}

This code will just show a black window with a blue circle in it and end when you press any key. Not very fancy but we just want to check if all runs OK.

Eclipse CDT comes with an option to build automatically, but I always disable it deselecting “Project->Build Automatically”. Build all by pressing Control+B or going to “Project->Build All”. Before debugging/running our application we need to configure the debugging/running environment. Go to “Run->Debug Configurations…” and do a double click on “C/C++ Application”. It will create a new debug configuration, but “C/C++ Application” parameter may not be automatically configured (this happend to me), so use “Browse…” button to select a “Project”, select your current project, and now (if your application binary program does exist) all should be OK to push “Debug” button.

You should now be able to run and debug your OpenCV application. Using any other library with Eclipse CDT is pretty much the same.

Leave a comment ?

5 Comments.

  1. I get two errors by the time I finish evrything and I can’t figure out why.

    Error 1: fatal error: opencv2/photo/photo.hpp: No such file or directory.

    Error 2: make:***[main.o] Error 1

    Any help will be appreciated.

    • You’ll have to give me some more info to help you. How did you configure Eclipse CDT, were do you have your OpenCV installed, what does your code do, etc.

      The first error look if that photo.hpp file is really were you told in the “Include paths (-I) option. There should be a file with that name in “/usr/include/opencv2/photo/photo.hpp” or “/usr/local/include/opencv2/photo/photo.hpp”, those are the usual locations. But I don’t have that “photo” folder in my OpenCV installation…

      The second error is nothing by it self. Look for the real error somewhere before it.

  2. The photo error is what really caught me off course. The folder you are discribing is not in the opencv2 folder (I don’t know why.) All I did to eclipse was add C/C++ on it.

    Also, for the include path, “/usr/local/include/opencv2″ and “/usr/include/opencv2“ didn’t work for me, so I put “/home/programming/OpenCV-2.4.0/include/opencv” instead, which is where the opencv2 folder is located and it worked for me but I’m not sure if that is what is causing the error.

    Thank You.

  3. Hola, Tengo un problema, no me muestra ninguna imagen al ejecutar, pero lo compila sin ningún error. durante el lauching solo muestra esto en la consola
    “Info: Nothing to build for …….” luego al terminar desaparece y no me muestra nada…
    Sabes que puede estar faltando????? Te voy a agradecer la respuesta.

    • Pues diría que lo que pasa es que no estás dándole a ejecutar, sino a compilar una y otra vez. Por eso el mensaje que dice “Nada que construir”, porque ya está compilado. Asegúrate de que estás haciendo click sobre ejecutar y no sobre compilar.

      Espero que te sirva la respuesta.

Reply to Diego ¬
Cancel reply