Problems with compiling the `displayImage`-example

asked 2013-03-31 20:48:16 -0500

Hi there!

I've installed OpenCV by this script - - then I tried to compile the DisplayImage-example (see here: but that doen not work.

This is what happens -both by following instructions which are given in the above link, both by compiling by hand- :

$ g++ `pkg-config --cflags opencv` DisplayImage.cpp \
  `pkg-config --libs opencv` -o DisplayImage.x

/tmp/ccTXxKFT.o: In function `main':
DisplayImage.cpp:(.text+0x75): undefined reference to `cv::imread(std::string const&, int)'
DisplayImage.cpp:(.text+0x116): undefined reference to `cv::namedWindow(std::string const&, int)'
DisplayImage.cpp:(.text+0x187): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status

How could I solve this problem?

Post scriptum: if I try to compile the following code

// trialcam1a.cpp
// A Simple Camera Capture Framework
// This program will connect to a camera then
// show the frames in a window
#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main()
  IplImage *frame = NULL; //Preparing frame pointer
  int key;

  //Allocates and initializes cvCapture structure
  // for reading a video stream from the camera.
  //Index of camera is -1 since only one camera
  // connected to the computer or it does not
  // matter what camera to use.
  CvCapture *input_camera = cvCaptureFromCAM(1);

  // Line added to test if the we're
  // getting something from the camera
  if (!input_camera)
      std::cout << "Error at capture";
      return 1;
      std::cout << "Camera grasped!"
                << std::endl;

  //Grabs and returns a frame from camera
  frame = cvQueryFrame(input_camera);
  #ifdef DEBUG
  int frame_index = 0;
  std::cerr << "frame #"
            << frame_index
            << " == "
            << frame << std::endl;

  //Creates window for displaying the frames
  //Flag is reset (0) --> change window size
  // manually
  cvNamedWindow("Capturing Image ...");

  //Change to the appropriate size. In GTK, the
  // inappropriate size will return a segmentation
  // fault. I don't know why ...
  //Gets the appropriate size using cvGetCaptureProperty
  // as property_id
  cvResizeWindow("Capturing Image ...",
         (int) cvGetCaptureProperty(input_camera, CV_CAP_PROP_FRAME_HEIGHT),
         (int) cvGetCaptureProperty(input_camera, CV_CAP_PROP_FRAME_WIDTH));

  cvSaveImage("first_frame.jpg", frame);

  while(frame != NULL)
      // This function vertically
      // flips the `CvArr` called
      // `frame` -what's a `cvArr`?,
      // see reference guide.
      cvFlip(frame, NULL, -1);
      //Shows a frame
      cvShowImage("Capturing Image ...", frame);

      //Checks if ESC is pressed and gives a delay
      // so that the frame can be displayed properly
      key = cvWaitKey(10);
      if(key == 27)
      //Grabs and returns the next frame
      frame = cvQueryFrame(input_camera);
      #ifdef DEBUG
      std::cerr << "frame #"
                << frame_index
                << " == "
                << frame << std::endl;

  //Release cvCapture structure

  //Destroy the window
  cvDestroyWindow("Capturing Image ...");

  return 0;

by typing

g++ trialcam1a.cpp -o trialcam1a.x `pkg-config --cflags --libs opencv`

I can produce a working executable.

Any idea of how could I solve that?

Thank you in advance,


EDIT: I'm working on a 64bit machine, with Ubuntu 12.10.

edit retag flag offensive close merge delete


I think that the compiler can find the path to cv and highgui, but not the path to opencv2 modules. Type the command pkg-config --cflags --libs opencv and see what it returns.

icedecker gravatar imageicedecker ( 2013-04-01 06:31:03 -0500 )edit

Also I am using open cv 3.1.0 version on ubuntu 14.04 and I am getting the same linking errors after compilation. Please suggest me what should I do? Please Please help me...!!

jayshree gravatar imagejayshree ( 2016-10-02 05:52:35 -0500 )edit