对'main'的未定义引用 - 但包括main函数

时间:2014-01-27 15:19:40

标签: c++ linux opencv main undefined-reference

尝试编译代码时出现以下错误:

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

我使用以下命令:

g++ detectTemplatePoints.cpp -o SURF_TemplatePoints `pkg-config --cflags --libs opencv`

从我在网上找到的内容看来,这似乎发生在你没有包含main入口点但是我确实有这个时,我的代码如下:

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;

void readme();
int main (int argc, char* argv[]) {
  if( argc != 2 ) {
    readme(); return -1; 
  }
  Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  if( !img_1.data ) { 
    std::cout<< " --(!) Error reading images " << std::endl; return -1; 
  }
  int minHessian = 400;
  SurfFeatureDetector detector( minHessian );
  std::vector<KeyPoint> keypoints_1;
  detector.detect( img_1, keypoints_1 );
  Mat img_keypoints_1;
  drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
  imshow("Keypoints 1", img_keypoints_1 );
  waitKey(0);
  return 0;
}
void readme() {
  std::cout << " Usage: ./detectTemplatePoints <img1>" << std::endl;
}

导致此错误的原因是什么?

2 个答案:

答案 0 :(得分:1)

正如错误消息所示:您没有main功能。他们必须具有以下签名之一:

int main()

int main(int, char**)

答案 1 :(得分:0)

首先,您应该使用g++ detectTemplatePoints.cpp -o SURF_TemplatePoints $(pkg-config --cflags --libs opencv)来链接opencv库 此外,您还没有向我们展示您所包含的库。 我可以编译得这样:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>

using namespace std;
using namespace cv;

void readme();

int main (int argc, char** argv) {
  if( argc != 2 ) {
    readme(); return -1; 
  }
  Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  if( !img_1.data ) { 
    std::cout<< " --(!) Error reading images " << std::endl; return -1; 
  }
  int minHessian = 400;
  SurfFeatureDetector detector( minHessian );
  std::vector<KeyPoint> keypoints_1;
  detector.detect( img_1, keypoints_1 );
  Mat img_keypoints_1;
  drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
  imshow("Keypoints 1", img_keypoints_1 );
  waitKey(0);
  return 0;
}
void readme() {
  std::cout << " Usage: ./detectTemplatePoints <img1>" << std::endl;
}