我一直在寻找,但我无法找到明确的疑问。
我正在尝试用c ++定义自己的类,我的类使用opencv中的库。
我创建了一个file.h文件,我只用声明来声明这些函数。
我创建了一个file.cpp文件,其中我解释了函数的外观。在这个程序中,我使用了我将在普通的opencv程序中使用的所有包含。 (我认为这是正确的)+包含file.h。
通常我会编译我的opencv程序,如:
g ++ -o program.cpp pkg-config --cflags opencv
pkg-config --libs opencv
现在我尝试以相同的方式编译我的file.cpp,以便在oder主文件中使用该类,但是我得到了一个错误。
下一步,一旦我有编译的类将是:
g ++ -o programMain.cpp compiledClass.o pkg-config --cflags opencv
pkg-config --libs opencv
任何帮助/建议都会很好,因为这是我第一次使用这么大的程序。
#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
这是我的文件segmentator.h。
在segmentator.c中我有:
#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(//param.){CODE}
vector<Mat*> separate(//param){CODE}
然后我正在编译像g ++ -o segmentator.cpp pkg-config --cflags opencv那样它并没有识别opencv库的扩展。
我将问题移到了显示的新问题:Not possible to compile. Headers files.Enclosed own objects definition
答案 0 :(得分:1)
通常,您首先将file.cpp编译为目标文件:
g ++ -c file.cpp pkg-config --cflags opencv
这将生成file.o
,然后您可以使用它来编译和链接main:
g ++ programMain.cpp file.o -o programMain pkg-config --cflags opencv pkg-config --libs opencv
您应该将file.h
中的包含限制为您严格需要的内容。同样适用于file.cpp
。
修改:查看代码,您需要执行以下操作:
cv::Mat
中添加cv::Point
和segmentator.h
的标头。我认为这些opencv2/core/core.hpp
虽然对我来说opencv/cv.h
在OpenCV 2.3.1上没问题。vector
segmantator.h
segmentator.c
包含main
函数,则需要在OpenCV库中进行链接,
g ++ segmentator.cpp -o segmentator pkg-config --cflags opencv pkg-config --libs opencv
如果您的segmentator.c
没有main
,即不能是可执行文件,您可以将其编译成目标文件,以后可以使用它来构建应用程序:
g ++ -c segmentator.cpp pkg-config --cflags opencv