在函数_start中,'未定义对main的引用',opencv,全景拼接

时间:2016-07-22 06:41:04

标签: c++11 makefile opencv3.0 opencv-stitching

我在opencv3.0.0-alpha下尝试示例代码时遇到以下错误:

ps@hp-pavilion:~/cvit/opencv_projects$ make stitch

g++ `pkg-config --cflags opencv` -o stitch stitch.cpp `pkg-config --libs opencv`
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
/build/glibc-qbmteM/glibc-2.21/csu/../sysdeps/x86_64/start.S:114: undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:5: recipe for target 'stitch' failed
make: *** [stitch] Error 1

我只是从opencv / samples / cpp中复制粘贴了stitching.cpp文件并重命名并将其作为stitch.cpp放在我的项目文件夹中,我也有我的makefile。 makefile看起来像:

CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`

% : %.cpp
    g++ $(CFLAGS) -o $@ $< $(LIBS)

我只是通过

编译.cpp文件,例如temp.cpp
make temp

每次都很完美。但是使用这个特殊的拼接代码,每次都会弹出错误。以下是示例代码 -

#include <iostream>
#include <fstream>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"

using namespace std;
using namespace cv;

bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "stitch_result.jpg";

void printUsage();
int parseCmdArgs(int argc, char** argv);

int main(int argc, char* argv[])
{
    int retval = parseCmdArgs(argc, argv);
    if (retval) return -1;

    Mat pano;
    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    Stitcher::Status status = stitcher.stitch(imgs, pano);

    if (status != Stitcher::OK)
    {
        cout << "Can't stitch images, error code = " << int(status) << endl;
        return -1;
    }

    imwrite(result_name, pano);
    return 0;
}


void printUsage()
{
    cout <<
        "Rotation model images stitcher.\n\n"
        "stitching img1 img2 [...imgN]\n\n"
        "Flags:\n"
        "  --try_use_gpu (yes|no)\n"
        "      Try to use GPU. The default value is 'no'. All default values\n"
        "      are for CPU mode.\n"
        "  --output <result_img>\n"
        "      The default is 'result.jpg'.\n";
}


int parseCmdArgs(int argc, char** argv)
{
    if (argc == 1)
    {
        printUsage();
        return -1;
    }
    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
        {
            printUsage();
            return -1;
        }
        else if (string(argv[i]) == "--try_use_gpu")
        {
            if (string(argv[i + 1]) == "no")
                try_use_gpu = false;
            else if (string(argv[i + 1]) == "yes")
                try_use_gpu = true;
            else
            {
                cout << "Bad --try_use_gpu flag value\n";
                return -1;
            }
            i++;
        }
        else if (string(argv[i]) == "--output")
        {
            result_name = argv[i + 1];
            i++;
        }
        else
        {
            Mat img = imread(argv[i]);
            if (img.empty())
            {
                cout << "Can't read image '" << argv[i] << "'\n";
                return -1;
            }
            imgs.push_back(img);
        }
    }
    return 0;
}

编辑:我刚尝试从samples文件夹本身运行示例代码并且它可以工作。如果我把它放在opencv / samples / cpp文件夹中,makefile运行完美而没有任何错误,但是当我复制时它没有 - 将它粘贴到另一个位置。

1 个答案:

答案 0 :(得分:2)

错误意味着链接器无法找到您的main功能。即使stitch.cpp定义main(我假设),链接器也无法找到它。原因尚不清楚,因为你构建Makefile的方式。我会做出这些改变:

  • 对C ++使用CXXFLAGS和/或CPPFLAGS,因为CFLAGS适用于C.
  • 展开make中的pkg-config输出,以便您可以看到要求编译器执行的操作。

您的Makefile将如下所示:

 CXXFLAGS = $(shell pkg-config --cflags opencv)
 LIBS = $(shell pkg-config --libs opencv)

 % : %.cpp
    g++ $(CXXFLAGS) -o $@ $^ $(LIBS)

我想认为,通过这些变化,问题的实际来源将变得明显。