在C ++和OpenCV中的其他文件中调用函数 [英] Call functions in other files in C++ and OpenCV
问题描述
我知道这可能是一个可能的重复,但我已经经历了几个帖子SO来尝试和解决这个问题
在OpenCV和C ++中有以下代码
I have the following code in OpenCV and C++
test.cpp
test.cpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include "draw_shapes.h"
using namespace cv;
using namespace std;
int main(){
Mat normal = imread("/INPUT 2.jpg");
Mat gray;
cvtColor(normal, gray,CV_RGB2GRAY);
Mat binary = gray > 128;
Point start = cv::Point(5.0,5.0);
Point end = cv::Point(200.0,200.0);
draw_line(binary, start, end, 0, 255, 0);
imshow("Draw_Line", binary);
while(waitKey(0)!=27){
;
}
return 0;
}
draw_shapes。 cpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include "draw_shapes.h"
void draw_line(cv::Mat img, cv::Point start, cv::Point end, int B, int G, int R){
int thickness = 2;
int lineType = 8;
line(img,start,end,Scalar(B,G,R),thickness,lineType);
}
void draw_rectangle(cv::Mat img, cv::Point p1, cv::Point p2, int B, int G, int R){
int thickness = 2;
int lineType = 8;
rectangle(img, p1, p2, Scalar(B,G,R),thickness, lineType);
}
draw_shapes。 h
#ifndef DRAWSHAPES_H
#define DRAWSHAPES_H
void draw_line(cv::Mat, cv::Point, cv::Point, int, int, int);
void draw_rectangle(cv::Mat, cv::Point, cv::Point, int, int, int);
#endif
compile_opencv.sh
compile_opencv.sh
#!/bin/bash
echo "compiling $1"
if [[ $1 == *.c ]]
then
gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"
./${1%.*}
通过几乎每个帖子在StackOverflow相关的头文件和包括外部CPP文件的功能,这是我得到的。
现在,当我编译,我得到
未定义的引用'draw_line(cv :: Mat,cv :: Point_< int> cv :: Point_< int> ;,int,int,int)'
我猜想这可能是因为我不编译draw_shapes。 cpp单独,但我试过一个简单的 int add(int x,int y)
在C文件,它直接工作。
I am taking a wild guess and saying that this might be because I am not compiling draw_shapes.cpp separately, but I tried a simple int add(int x, int y)
in a C file and it worked directly. Where am I going wrong here?
推荐答案
正如@Ben所说,你需要编译 draw_shapes .cpp
文件。如果你想单独编译你的文件,你需要使用 -c
开关将它们编译为对象,然后将它们链接在一起。 您的第二个主题中的进一步说明
Just as @Ben mentioned, You need to compile the draw_shapes.cpp
file. The twist is, if You want to compile Your files separately You need to compile them as objects with -c
switch, and then link them together. Further explanation in Your second thread
这篇关于在C ++和OpenCV中的其他文件中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!