包含OpenCV 2.4.9的Visual C++编译错误

Visual C++ with included OpenCV 2.4.9 compile error

本文关键字:Visual C++ 编译 错误 OpenCV 包含      更新时间:2023-10-16

我正试图编写一段代码,在Visual Studio 2010环境中的windows窗体上显示来自摄像机的视频。下面是我的代码:

#pragma once
#include <opencvcv.h>
#include <opencvhighgui.h>
extern CvCapture* cam;
extern IplImage* image,*image_copy;
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
extern const char* cascade_name;
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {

         cam=cvCaptureFromCAM(-1);
         image=cvQueryFrame(cam);
         if ((videoBox->Image==nullptr))
         {
             Bitmap^img1=gcnew Bitmap(videoBox->Width,videoBox->Height);
             videoBox->Image=img1;
         }
         cascade=(CvHaarClassifierCascade*)cvLoad(cascade_name,0,0,0);
         storage=cvCreateMemStorage(0);
         if(cam)
         {
             {
                 if(!image_copy)
                     image_copy=cvCreateImage(cvSize(image->width,image->height),IPL_DEPTH_8U,image->nChannels);
                 if(image->origin==IPL_ORIGIN_TL)
                     cvCopy(image,image_copy,0);
                 else cvFlip(image,image_copy,0);
                 //khai bao mang mau
                     static CvScalar colors[] = 
                     {
                         {{0,0,255}},//red
                         {{0,128,255}},//orange
                         {{0,255,255}},//cyan
                         {{0,255,0}},//green
                         {{255,128,0}},//blue
                         {{255,255,0}},//yellow
                         {{255,0,0}},//blue+
                         {{255,0,255}}//pink
                     };
                     double scale = 1.3;
                     IplImage* gray = cvCreateImage( cvSize(image_copy->width,image_copy->height), 8, 1 );
                     IplImage* small_img = cvCreateImage( cvSize( cvRound (image_copy->width/scale),cvRound (image_copy->height/scale)),8,1);
                     //int i;
                     cvCvtColor( image_copy, gray, CV_BGR2GRAY );
                     cvResize( gray, small_img, CV_INTER_LINEAR );
                     cvEqualizeHist( small_img, small_img );
                     cvClearMemStorage( storage );
                 Graphics^ g = Graphics::FromImage(videoBox->Image);
                 Bitmap^ anh2 = gcnew Bitmap(image_copy->width,image_copy->height,image_copy->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb, IntPtr(image_copy->imageData));
                 g->DrawImage(anh2, (videoBox->Width-image_copy->width)/2, (videoBox->Height-image_copy->height)/2);
                 videoBox->Refresh();
                 delete(g);
             }
         }
     }

};}

起初,我的电脑上安装了.NET Framework 4.5.1。但当我编译代码时,我收到了这个错误:

"链接:致命错误LNK1123:转换为COFF期间失败:文件无效或损坏"

我寻找解决方案,发现VisualStudio2010只适用于.NETFramework4。我卸载了4.5.1版本,安装了4版本。但在那之后,我收到了这个错误:

1>Manual_Mode.obj : error LNK2020: unresolved token (0A0007F1) "extern "C" void __cdecl cvClearMemStorage(struct CvMemStorage *)" (?cvClearMemStorage@@$$J0YAXPAUCvMemStorage@@@Z)
1>Control_Interface.obj : error LNK2028: unresolved token (0A0007FF) "extern "C" void __cdecl cvClearMemStorage(struct CvMemStorage *)" (?cvClearMemStorage@@$$J0YAXPAUCvMemStorage@@@Z) referenced in function "private: void __clrcall Control_Interface::Manual_Mode::timer1_Tick(class System::Object ^,class System::EventArgs ^)" (?timer1_Tick@Manual_Mode@Control_Interface@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Control_Interface.obj : error LNK2019: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@$$FYAHPAHH@Z) referenced in function "public: void __thiscall cv::SparseMat::addref(void)" (?addref@SparseMat@cv@@$$FQAEXXZ)
1>Manual_Mode.obj : error LNK2001: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@$$FYAHPAHH@Z)
1>C:UsersadminDesktopControl_InterfaceDebugControl_Interface.exe : fatal error LNK1120: 51 unresolved externals

你能告诉我怎么处理吗?

您可以尝试将代码进一步减少到最低限度,在询问此处之前,您应该在将来这样做。无论如何,我想从一个简短的角度来看,问题是你的"extern"告诉编译器在某个地方有一个给定类型和名称的对象,但你必须确保它真的存在。您可能想要的是告诉编译器创建这样一个对象(注意,根据C++对象模型,指针是一个对象),这可以通过删除"extern"来实现。

如果将来由于多重定义的符号而导致链接器错误,请执行相反的操作:只留下一个没有extern的位置,所有其他位置都有extern。