I know this might be a possible duplicate but I have actually gone through several posts on SO to try and solve this problem
我知道这可能是一个副本,但我实际上已经通过了几个帖子试图解决这个问题
I have the following code in OpenCV and C++
我在OpenCV和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
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
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%.*}
I have gone through almost every post on * related to header files and including functions from external CPP files and this is what I got. Now, when I compile, I getundefined reference to 'draw_line(cv::Mat, cv::Point_<int>, cv::Point_<int>, int, int, int)'
我在*上浏览了几乎所有与头文件相关的文章,包括来自外部CPP文件的函数,这就是我得到的。现在,当我编译时,我得到了“draw_line(cv: Mat, cv: Point_
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?
我大胆地猜测这可能是因为我没有编译draw_shapes。单独使用cpp,但是我在一个C文件中尝试了一个简单的int add(int x, int y),它直接工作。我哪里出错了?
2 个解决方案
#1
0
(Based on Ben's comment)
(基于本的评论)
This command should work:
这个命令应该工作:
g++ -ggdb `pkg-config --cflags opencv` test.cpp draw_shapes.cpp `pkg-config --libs opencv`
Note the backquotes around "pkg-config --cflags opencv" and "pkg-config --libs opencv". Reqular quotes ('), won't work - you really need backquotes here (`). The backquote key is just below the "Esc" key on most keyboards. Or just copy-paste them from your script.
请注意“pkg-config—cflags opencv”和“pkg-config—libs opencv”的反引号。Reqular引号(')不起作用——您确实需要这里的backquotes(')。backquote键位于大多数键盘上的“Esc”键的下方。或者直接从脚本中复制粘贴它们。
#2
0
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
正如@Ben提到的,您需要编译draw_shapes。cpp文件。难点在于,如果想要单独编译文件,需要使用-c开关将它们编译为对象,然后将它们链接在一起。在你的第二条线索中作进一步的解释
#1
0
(Based on Ben's comment)
(基于本的评论)
This command should work:
这个命令应该工作:
g++ -ggdb `pkg-config --cflags opencv` test.cpp draw_shapes.cpp `pkg-config --libs opencv`
Note the backquotes around "pkg-config --cflags opencv" and "pkg-config --libs opencv". Reqular quotes ('), won't work - you really need backquotes here (`). The backquote key is just below the "Esc" key on most keyboards. Or just copy-paste them from your script.
请注意“pkg-config—cflags opencv”和“pkg-config—libs opencv”的反引号。Reqular引号(')不起作用——您确实需要这里的backquotes(')。backquote键位于大多数键盘上的“Esc”键的下方。或者直接从脚本中复制粘贴它们。
#2
0
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
正如@Ben提到的,您需要编译draw_shapes。cpp文件。难点在于,如果想要单独编译文件,需要使用-c开关将它们编译为对象,然后将它们链接在一起。在你的第二条线索中作进一步的解释