0

I have written the following code in C++ which use openCV to be run in Beaglebone:

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <netinet/in.h>
    #include "opencv/cv.h"
    #include "opencv/highgui.h" 

    using namespace cv;
    using namespace std;

    int main(int argc, char *argv[])
    {
         CvCapture *capture = 0;
         Mat img3;
         Mat src;

         capture = cvCaptureFromCAM(0);

         vector<int> p;
         p.push_back(CV_IMWRITE_PNG_COMPRESSION);
         p.push_back(9);

         while (1) {
              img3 = cvQueryFrame(capture);
              cvtColor(img3, img3, CV_BGR2GRAY);
              pyrDown(img3, src, Size( img3.cols/2, img3.rows/2 ) );
              if (!imwrite("/home/root/Desktop/website/fig3bmp.bmp",src,p)) {
                  printf("mat not saved!!!\n"); 
              }
         }

         return 0; 
    }

I have tried compiling the code using: "g++ -o CamaraTest CamaraTest.cpp", but it does not work, and all the errors I get are something like: "undefined reference to: cv... "

I have already checked that the files "cv.h" and "highgui.h" are in the directory "/usr/include/opencv".

How can I compile this code? Any suggestion would help a lot.

Thanks in advance.

gus.

gus
  • 355
  • 2
  • 5
  • 19

1 Answers1

1

These "undefined reference to: cv... " messages are linker errors due to missing libraries - you need to link with the OpenCV libraries in your g++ command line, e.g.:

$ g++ -Wall -g -o CamaraTest CamaraTest.cpp `pkg-config --cflags --libs opencv` 
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • I've answered this very same question a gazillion times. The problem is that people don't use the search box. lol – karlphillip Jan 28 '13 at 16:02
  • Very true. Also a lot of people don't seem to understand the difference between compiling and linking, or the roles of includes versus libraries. – Paul R Jan 28 '13 at 16:03
  • Thanks for the reply. It works, but now the problem is that when I run the code using "./CamaraTest" I have the message saying: "Segmentation fault". How can I fix this? – gus Jan 28 '13 at 16:06
  • 1
    Well that's a different problem. Try to debug it, and if you get stuck then ask a new question. – Paul R Jan 28 '13 at 16:08
  • One more thing - **always** compile with warnings enabled, and pay attention to any compiler warnings. `g++ -Wall ...` – Paul R Jan 28 '13 at 16:10