0

I am trying to read an image in OpenCV, like this:

#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>



using namespace cv;
using namespace std;




int main (int argv, char **argc)
{
    Mat image = imread("Foam_Image.jg", CV_LOAD_IMAGE_GRAYSCALE);

    return 0;
}

But I get the following error:

undefined reference to cv::imread(cv::String const&, int)

It seems that OpenCV cannot find the libraries I included, maybe because I didn't link them correctly, or maybe there are some libraries missing. Does anyone know how to look for missing libraries or how to link the libraries in OpenCV?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Bowecho
  • 899
  • 2
  • 8
  • 17
  • 2
    What is your compiling line ? – Dimitri Mockelyn Jul 01 '15 at 11:35
  • Which platform and compiler are you using? – wrren Jul 01 '15 at 11:36
  • I use the GNU GCC compiler embedded in OpenCV. The compilation goes well, though. Only when I build and run it I get this error. – Bowecho Jul 01 '15 at 11:59
  • You need to add appropriate `-L` and `-l` switches in order to link the OpenCV libraries. – Paul R Jul 01 '15 at 12:01
  • @Paul R How do I do this on Windows 7? – Bowecho Jul 01 '15 at 14:59
  • Oh - you neglected to mention that you're on Windows - commiserations. ;-) On a proper OS you'd add the switches on the command line for simple projects, or in a Makefile for more complex projects. On Windows there is no standard method so you'll need to read the documentation. – Paul R Jul 01 '15 at 15:19

1 Answers1

1

If your operating system is any UNIX which has CMake, then it would be better for you to write a CMakelists.txt file as follows

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

And just use cmake . make ./DisplayImage

to execute the program.

you can install CMake from the official repositories using your package manager

In case your operating system is Windows, install CMake and set compiler options as Visual Studio (your version). Also add the OpenCV path to your system path, if not already done. More instructions here : http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html

Giridhur
  • 164
  • 7
  • 1
    suggesting just another tool for such a basic problem is not very useful IMO – Simon Jul 01 '15 at 11:48
  • I am using Windows 7. Sorry I didn't mention this. – Bowecho Jul 01 '15 at 12:01
  • @Simon I agree but even if it is windows, CMake does a pretty good job generating the sln file for visual studio. and linking the dlls one by one for every project isn't recommended. – Giridhur Jul 01 '15 at 12:10
  • 1
    @Giridhur Correct, CMake is a pretty good tool, however in this particular case it adds another level of complexity, which IMO is not useful for the OP to understand this linking issue – Simon Jul 01 '15 at 12:47