0

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++

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

#!/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 StackOverflow related to header files and including functions from external CPP files and this is what I got. Now, when I compile, I get
undefined reference to 'draw_line(cv::Mat, cv::Point_<int>, cv::Point_<int>, int, int, int)'

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?

Kanishka Ganguly
  • 1,252
  • 4
  • 18
  • 38

2 Answers2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Oleg
  • 1,037
  • 7
  • 13
  • 1
    Generally, it is better to suggest the use of `$(…)` in place of backquotes (and I'm not even going to try to fight my way around Markdown to show backquotes in this comment). You might also note that this creates `a.out` as the executable. – Jonathan Leffler Jun 27 '14 at 04:12
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

Community
  • 1
  • 1
morynicz
  • 2,322
  • 2
  • 20
  • 34