Ask Your Question
0

Simple Opencv program with link error on

asked 2012-08-02 09:48:15 -0600

feelfree gravatar image

updated 2012-08-02 10:07:23 -0600

Hi everyone, I am a Opencv Newbie. I have downloaded and installed Opencv 2.4 according to the instruction, and after that I began writing my first Opencv program, which was basically a copy of the tutorial on the web.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std; 

int main( int argc, char** argv )
{ 
    if( argc != 2) 
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

However, the program has several link errors although compiling seems fine. The link errors I have received are as follows:

Error   2   error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class stlp_std::basic_string<char,class stlp_std::char_traits<char>,class stlp_std::allocator<char> > const &,int)" (?namedWindow@cv@@YAXABV?$basic_string@DV?$char_traits@D@stlp_std@@V?$allocator@D@2@@stlp_std@@H@Z) referenced in function _main    C:\Research\OpencvTest\OpencvTest.obj
Error   1   error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class stlp_std::basic_string<char,class stlp_std::char_traits<char>,class stlp_std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DV?$char_traits@D@stlp_std@@V?$allocator@D@2@@stlp_std@@ABV_InputArray@1@@Z) referenced in function _main    C:\Research\OpencvTest\OpencvTest.obj

Could anyone tell me what's wrong with my program? Thanks!

EDIT: A very tricky part of the problem is that if I use old-style Opencv function I will have no problem at all. The following codes can run very well:

int main( int argc, char** argv )
{
    char* filename = "C:\\Research\abc.pgm";  
     IplImage *img0;

    if( (img0 = cvLoadImage(filename,-1)) == 0 )
        return 0;

    cvNamedWindow( "image", 0 );
    cvShowImage( "image", img0 );
    cvWaitKey(0);  
    cvDestroyWindow("image");
    cvReleaseImage(&img0);



    return 0;
}
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2012-08-02 15:05:15 -0600

mms gravatar image

You're mixing different implementations of STL. VC10 (for opencv) and STLPort (for your code).

edit flag offensive delete link more

Comments

Thanks, this is the problem. Could you suggest a solution to solve this problem?

feelfree gravatar imagefeelfree ( 2012-08-03 02:37:57 -0600 )edit

Why do you insist on using STLPort?

mms gravatar imagemms ( 2012-08-03 03:47:53 -0600 )edit
0

answered 2012-08-02 09:58:13 -0600

jasedit gravatar image

What libraries are you linking in? How have you set up this application to build? It looks as though the highgui library isn't being linked in, which would be necessary to make things work. How to do this depends on how you are building the application - either through Visual Studio (in which case you need to use the GUI to specify the OpenCV highgui library as something to link in) or through the command line (in which case you need to locate the library and modify the command line appropriately.)

edit flag offensive delete link more

Comments

Thanks, I am now using visual studio 10, and I have put the libraries as well as their path in the searching path. The tricky part is that if I use the c style code, it is successful. I will recognize my question to make it clear.

feelfree gravatar imagefeelfree ( 2012-08-02 10:03:57 -0600 )edit

I don't believe putting the libraries on the search path is enough to link them in. You need to actually add the library as a dependency of the project. This forum post seems to explain how to do it: http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/09246868-587e-4980-98a4-e8860276913b

jasedit gravatar imagejasedit ( 2012-08-02 10:10:16 -0600 )edit

Thanks, I looked through the post you suggested, and I am sure that I have done exactly people suggested in the post.

feelfree gravatar imagefeelfree ( 2012-08-02 10:13:45 -0600 )edit

Searching the errors you listed produces this post: http://stackoverflow.com/questions/9867514/opencv-build-on-visual-studio-link-error which includes several answers purported to help. I'm assuming the 230 part of the .lib files needs to be changed, but perhaps it provides some insight?

jasedit gravatar imagejasedit ( 2012-08-02 10:29:26 -0600 )edit

Thanks, but I have already added opencv_highgui242d.lib opencv_core242d.lib in the program.

feelfree gravatar imagefeelfree ( 2012-08-02 10:55:52 -0600 )edit
0

answered 2012-08-02 14:17:03 -0600

awknaust gravatar image

updated 2012-08-02 14:18:25 -0600

This really sounds like you missed some step of the installation process (the one where you tell your project about *.lib ). Did you read this tutorial? I suggest re-reading it making sure you have followed all steps closely

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-08-02 09:48:15 -0600

Seen: 8,823 times

Last updated: Aug 02 '12