Opencv函数只能以C代码方式调用,而不能以C ++方式调用

我是Opencv的新手。 根据说明下载并安装Opencv 2.4后,我开始编写我的第一个Opencv程序,该程序基本上是Web上教程的副本。

#include  #include  #include  #include "cv.h" #include "highgui.h" #include  #include  #include  using namespace std; using namespace cv; 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; } 

代码工作得很好,但您可能会注意到在上面的代码中调用Opencv函数是C代码方式。 因此,我决定使用以下代码继续使用C ++代码方式:

 #include  #include  #include  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; } 

但是,在这种情况下,程序有几个链接错误,虽然编译似乎很好。 我收到的链接错误如下:

 Error 2 error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class stlp_std::basic_string<char,class stlp_std::char_traits,class stlp_std::allocator > 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,class stlp_std::allocator > 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 

我很确定我在程序中添加了必要的Opencv库(我使用VC10),我添加的其他库如下:

 stl_port.lib opencv_highgui242d.lib opencv_core242d.lib 

我想知道我的设置有什么问题。 为什么它适用于第一个程序而不是第二个程序? 任何想法将不胜感激。 谢谢!

它与混合STLPort和MSVC STL有关。 您可能没有自己构建OpenCV库,因此他们使用的是VC10 STL。 使用C接口只有char*但是C ++接口链接器与方法中的std::string混淆。 如果你将它的输入转换为string你应该看到与imread相同的结果。

我可以在项目中混合使用STL实现吗?