OpenCV DevC++

I am new to programming c++. I am using DevC++ 4.9.9.2. I am trying to use OpenCV 2.2 with it. I need to use both DevC++ and OpenCV on several different computers so I installed both on my USB. I ran some simple console projects using DevC++ and they executed fine. I followed the instructions at http://opencv.willowgarage.com/wiki/DevCpp
to setup OpenCV but when I ran a sample OpenCV program provided in Learning OpenCV: Computer
Vision with the OpenCV Library
I kept on getting the following error:

[Linker error] undefined reference to 'cvLoadImage'
[Linker error] undefined reference to 'cvNamedWindow'
[Linker error] undefined reference to 'cvShowImage'
[Linker error] undefined reference to 'cvWaitKey'
[Linker error] undefined reference to 'cvReleaseImage'
[Linker error] undefined reference to 'cvDestroyWindow'
Id returned 1 exit status
F:\CPP\something\Makefile.win [Build error] [something.exe] Error 1

The project's name is something and this is the code for it:
1
2
3
4
5
6
7
8
9
10
11
#include "highgui.h"

int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( argv[1] );
    cvNamedWindow( "Example1" , CV_WINDOW_AUTOSIZE );
    cvShowImage( "Example1" , img );
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
}

I have no clue what to do to solve this error. I don't think there is a problem with the code. How do I fix it?
So it appears that highguy.h doesn't tell the linker which lib files are needed. I don't know which ones are needed, so wait for someone who knows. :-(

The most I can say is that you need one or more of the following line:

#pragma comment(lib, "<libfilename>")

Or seek help in the official OpenCV website.
Last edited on
You're not linking against the libraries. That page you linked to explains how to do it.

This sort of thing:
To finish, on the section Add the following ... write : -L"C:\Program Files\OpenCV\lib" -lcxcore -lcv -lcvaux -lhighgui -lml -lcvcam. Or the following for OpenCV 2.1 -L"C:\OpenCV2.1\lib" -lcxcore210 -lcv210 -lcvaux210 -lhighgui210 -lml210
Last edited on
I tried both in the extra commands box and I still got the error. I am using OpenCV 2.2 and it says 2.1
Is there commands for OpenCV 2.2? or do I need to do something else?

on the pragma thing, is the libfilename the name of the header file or the .lib file?


A header file generally contains just enough so that your code will compile. Definitions of structures and classes that you might use, and the prototypes of functions. This is enough for your compiler to do its job, effectively leaving big gaps with notes on saying "here, go and run this library function which I promise you can find in a library that we're about to link to".

A header file is text and gets inserted into your code, and compiled. The library is the already compiled code. You include header files in your code at compile time; you link to static libraries at link time and dynamic libraries at run time.

So, with this knowledge, it becomes clear that the pragma thing is meant to cause your linker to link to the right libraries.

There are two ways of linking to the right libraries. The first way is to tell your linker which libraries to link to. How do you know which libraries to link to? The library documentation tells you. How do you actually tell the linker? In your IDE, by putting the right library path name in the library path name box and the right library names in the library name box. How do you know the right path name? You installed the library yourself, so you ought to know where you put it.

The pragma method is a windows IDE thing, as far as I know, and it's meant to provide the exact same information to the linker as the other way. I've no experience of using the pragma to link against the libraries.

I am using OpenCV 2.2 and it says 2.1 Is there commands for OpenCV 2.2?


Yes there are. Find the path and names of the libraries in version 2.2 and put those in instead.

I looked around on various forums and opencv's site, but I couldn't find the paths or libraries. I did find out that Dev C++ has several hundered known bugs and hasn't been updated in several years. I downloaded Code::Blocks and within five minutes had OpenCV working.

Thanks anyway for all your help and advice.
Topic archived. No new replies allowed.