0

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

Matt Popovich
  • 236
  • 1
  • 2
  • 14
Ivánovick
  • 267
  • 1
  • 5
  • 17

1 Answers1

1

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

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I have tried, but without success. I have included files only in file.cpp, where i define the class' functions. I have tried to compile it like: g++ -o segmentator.cpp `pkg-config --cflags opencv` with these commas. It says something like" there are not input files" which I dont understand. I am gonna try make some google as well. Many thanks for the attention. – Ivánovick May 23 '12 at 13:22
  • About the includes, I used all the includes that normally I am using when compiling a program on opencv – Ivánovick May 23 '12 at 13:25
  • @Ivánovick Well, if you can provide small .h, .cpp and main .cpp files that reproduce the problems people might be able to help. – juanchopanza May 23 '12 at 13:25
  • I tried to clarify it a little bit more. thanks. I dont know, it looks like it could be a linking library problem? – Ivánovick May 23 '12 at 13:42
  • @Ivánovick well, you miss some headers in `segmentator.h`, plus other small problems. I edited my answer. – juanchopanza May 23 '12 at 13:56
  • I am trying to do your suggestions, tried to include in segmentator.h top of the .h file: #include #include but not manage to make the compiler recognize it. Did you mean this? Thanks in advance juanchopanza – Ivánovick May 23 '12 at 15:01
  • Ok, @juanchopanza namespace was missing. Now, I think it is working. Many thanks. – Ivánovick May 23 '12 at 15:15
  • Hi again, I encountered one problem...maybe you could help me. Now the objects that I want to link are two. Once I create both of them object1.o object2.o I tried to link them like: g++ -o def image_reg.cpp object1.o object2.o `pkg-config --cflags opencv` `pkg-config --libs opencv`. Object 1 is always recognised but with two is not working, It is never linked. Any help would be nice, thanks in advance. – Ivánovick May 30 '12 at 15:00
  • g++ def image_reg.cpp -o segmentator.o homogra.o `pkg-config --cflags opencv` `pkg-config --libs opencv` is always not working. – Ivánovick May 30 '12 at 15:02
  • @Ivánovick the first version looks OK, the second one is wrong because `-o` should be folloewd by the name of the output of the compilation/linking command. – juanchopanza May 30 '12 at 15:13
  • Ok, No idea why but it is not working, I am gonna try to search something more. It is really strange. If I do the compilation with object 1 alone, without any kind of reference to object 2 everything is working fine.As well with object2. Then if I try to do the same like in version one (up) it is not recognising the second object that appears in the include== If I have in my .cpp, where the main is, #include "object 1" #include "object2" when I conpile is always and only recognising the object one, the two will never be recognised. Thanks – Ivánovick May 31 '12 at 07:11
  • @Ivánovick maybe you should prepare a **minimal** example that reproduces the problem, including your compilation lines, and put it in another question. This is all getting confusing! – juanchopanza May 31 '12 at 07:14