Search code examples
c++opencvcompilationincludeheader-files

compiling header file using opencv. Own class definition


I have been searching but could not find something clear for my doubt.

I am trying to define my own class in c++, my class uses the libraries from opencv.

I create a file.h file where I just declare the functions, with its guards.

I create a file.cpp file where I explain how the functions look like. In this program I used all the includes I would use in a normal opencv program. (I thought it was right) + the include file.h.

Normally I compile my opencv programs like:

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

Now I try to compile my file.cpp by the same way in order to use the class in oder main file but I obtain an error.

The next step, once I would have the compiled class would be:

g++ -o programMain.cpp compiledClass.o `pkg-config --cflags opencv` `pkg-config --libs opencv`

Any help/advice would be nice since it is the first time I am managing with such a big program.

#ifndef _NAMES_H  
#define _NAMES_H     

class segmentator {
public:
      
   void search(Mat img,
               vector<std::vector<cv::Point> >&contours,
               vector<int>&similarity);
   
   void similar(vector<std::vector<cv::Point> >&contours,
                vector<std::vector<cv::Point> >&contours2,
                vector<int>&similarity);
   
   vector<Mat*> separate(Mat img,
                         Mat img2,
                         vector<std::vector<cv::Point> >&contours,
                         vector<std::vector<cv::Point> >& contours2,
                         vector<int> idx);
};

#endif

This is my file segmentator.h.

In segmentator.c I have:

#include <stdio.h>
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "segmentator.h"

void segmentator::search(/*parameters*/){/*CODE*/}
void segmentator::similar(/*parameters*/){/*CODE*/}
vector<Mat*> separate(/*parameters*/){/*CODE*/}

And then I am compiling like this

g++ -o segmentator.cpp `pkg-config --cflags opencv`

and it is not recognising the extensions of opencv library.

I moved the question with the new problem that appeared to: Not possible to compile. Headers files.Enclosed own objects definition


Solution

  • Typically, you would first compile file.cpp into an object file:

    g++ -c file.cpp pkg-config --cflags opencv

    This produces file.o, which you then use to compile and link the main:

    g++ programMain.cpp file.o -o programMain pkg-config --cflags opencv pkg-config --libs opencv

    You should limit the includes in file.h to those you strictly need. Likewise for file.cpp.

    Edit: looking at your code, you need to do the following:

    • include the headers for cv::Mat and cv::Point in segmentator.h. I assume these would be opencv2/core/core.hpp although for me opencv/cv.h is fine on OpenCV 2.3.1.
    • include the header vector in segmantator.h
    • if your segmentator.c contains a main function, you need to link in the OpenCV libraries, so

      g++ segmentator.cpp -o segmentator pkg-config --cflags opencv pkg-config --libs opencv

    • if your segmentator.c does not have a main, i.e cannot be an executable, you can compile it into an object file, that you can use later to build applications:

      g++ -c segmentator.cpp pkg-config --cflags opencv