【问题标题】:undefined reference to `main' - but main function included [closed]未定义对“主”的引用-但包括主函数[关闭]
【发布时间】:2014-02-18 12:15:40
【问题描述】:

我在尝试编译我的代码时收到以下错误:

/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;
}

是什么导致了这个错误?

【问题讨论】:

  • 这不可能是你的完整代码,还有更多的东西,问题可能就在那里(就像一个宏发狂)
  • 第四行return -1的目的是什么?
  • pkg-config --cflags --libs opencv 输出什么?
  • @Colin747 嗯...对不起,如果这个太明显了,但我知道它以前发生在我身上,所以无论如何我都会问它。您是否有任何机会编辑了错误的文件?也就是说,“坏目录”上的文件实际上是一个没有 main 方法的旧版本,并且您不小心编辑了另一个文件,认为它是 detectTemplatePoints.cpp,而实际上它不是?
  • 我讨厌这种情况。当我有一个 SO 问题时,我更讨厌它。 :(

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


【解决方案1】:

首先,您应该使用 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;
}

【讨论】:

    【解决方案2】:

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

    int main()
    

    int main(int, char**)
    

    【讨论】:

    • 我在第二行声明了一个main 函数?
    • 添加了关于为什么您没有评论的解释
    • @PaulEvans 他的代码现在似乎有正确的主签名并且错误仍然存​​在,所以这不是原因。
    • @luiscubal 然后是编译命令
    • @PaulEvans:不是,它对我来说与那个编译命令配合得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多