Not possible to compile. Headers files.Enclosed ow

2019-07-28 08:22发布

问题:

Here I am with a similar question as the last time, and for which I could not find any answer.

Note that I consider important: Normally I compile my programs in opencv with the next command:

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

This line will create an executable whose name will be def and that I will be able to use.

I am working in a project, and as it was getting bigger, I had to define some objects, just to make everything easier and possible to handle. I create one object from the files: homogra.cpp and homogra.h the comand I used for it was:

g++ -c  homogra.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv`

Then, I wrote in my program.cpp the line #include "homogra.h"

And I compile like:

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

Until now everything is working fine.

Then I create a second object(with the same compilation line as for homogra, but this time with segmentator.cpp and segmentator.h), i wrote the line #include "segmentator.h",(in program.cpp) and I compile like:

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

Now it is not working, and it is not recognising segmentator. I checked already if segmentator was working and everything works fine if homogra is the only include in the program.cpp.

I notice something strange. If I change the lines and I write before,in the #include lines, #include "segmentator.h" and then #include "homogra.h", then the compiler, with the same line for compiling :

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

is only recognising this time segmentator and not homogra. It is maybe a little difficult to understand, I tried to explained it as better as possible.

Any help!?

Many thanks in advance.

Here is homogra.h:

using namespace cv;
using namespace std;

#ifndef _NAMES_H  
#define _NAMES_H   
class homogra {
public:

    Mat matCalculation( Mat img, Mat img2);
    void printMatrix(Mat matrix);

};
#endif

In homogra.cpp I have all the tipical includes and homogra.h:

#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 "homogra.h"

And then the functions explained.

Object 2 is segmentator.h

 using namespace cv;
 using namespace std;

#ifndef _NAMES_H  
#define _NAMES_H   
 class segmentator {
public:

    void search(Mat img,vector<std::vector<cv::Point> >& contours);

     void similar(vector<std::vector<cv::Point> >& contours,vector<std::vector<cv::Point> >& contours2,vector<int>& idx);

    vector<Mat> separate(Mat img,Mat img2,vector<std::vector<cv::Point> >&   contours,vector<std::vector<cv::Point> >& contours2,vector<int> idx);
};

#endif

And in segmentator.cpp I have again all the same includes, except homogra.h and instead of this one I have segmentator.h.

Program.cpp is image_reg.cpp:

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

#include "homogra.h"
#include "segmentator.h" 
 using namespace cv;
 using namespace std;

int main(int argc, char ** argv )
{ //Here is the code where I try to invoque two instances of homogra and segmentator.
}

If I let homogra.h as the first to be read in the includes list of image_reg.cpp then only homogra.h is recognised, if I let at the first position segmentator, then only segmentator.h instances would be created and homogra. h would not be recognised.

Thanks

回答1:

Your include header guards are wrong. They should be unique, using the name of the source file, rather than just _NAMES_H.

So in homogra.h you should have this:

#ifndef HOMOGRA_H  
#define HOMOGRA_H   
...
#endif

...and in segmentator.h, you should have this

#ifndef SEGMENTATOR_H  
#define SEGMENTATOR_H   
...
#endif

Also, it's really bad practice to have a using namespace xxx; in a header file. You make it very difficult for your headers to coexist with others.

As Jonathan Wakely points out, beginning symbols with underscores is not a great idea, either.