无法构建OpenCV文件

时间:2012-03-31 17:28:22

标签: opencv

我正在尝试构建此代码

#include "stdafx.h"
#include <iostream>

#include <math.h>
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{   
    int pixel;

    Mat matC1_32S;

    return 0;
}

我收到了一个错误:

1>c:\test1\test1\test1.cpp(21): error C2065: 'Mat' : undeclared identifier
1>c:\test1\test1\test1.cpp(21): error C2146: syntax error : missing ';' before identifier 'matC1_32S'
1>c:\test1\test1\test1.cpp(21): error C2065: 'matC1_32S' : undeclared identifier

我还应该包括哪些内容?还是别的什么?

3 个答案:

答案 0 :(得分:3)

您没有为Mat提供命名空间。如果在编译时链接到OpenCV库,这将起作用:

#include "stdafx.h"
#include <iostream>

#include <math.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
   int pixel;

   cv::Mat matC1_32S;

   return 0;
}

或者您可以在using namespace cv;之前添加_tmain,这样您就不必为每个外观添加前言。

另外,你过度使用了#include语句。您不需要* _c.h文件。 (也许你在试图找出未宣布Mat的原因时添加了那些。)

答案 1 :(得分:0)

感谢您的帮助,但为了使其成功,我实际上还要包含以下内容

#ifdef _DEBUG 
  #pragma comment(lib, "opencv_core231d.lib") 
  #pragma comment(lib, "opencv_highgui231d.lib") 
  #pragma comment(lib, "opencv_imgproc231d")
  #pragma comment(lib, "opencv_objdetect231d.lib")

#else 
  #pragma comment(lib, "opencv_core231.lib") 
  #pragma comment(lib, "opencv_highgui231.lib") 
  #pragma comment(lib, "opencv_imgproc231.lib")
  #pragma comment(lib, "opencv_objdetect231.lib")

#endif 

我理解为什么我需要'使用命名空间cv,但为什么我需要pragma这个东西,尽管我在项目属性中提供了库路径。 ('我正在使用VisualStudio 10

答案 2 :(得分:0)

您必须转到properties >> C/C++ >> Avanced >> Compile As并选择Compile as C++ code /TP 我做的。 它有效。