2016-07-22 71 views
1

我嘗試下opencv3.0.0-α的樣本代碼,而我遇到了以下錯誤:在功能_start,「未定義的引用主」,OpenCV的,全景拼接

[email protected]:~/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放置在我的項目文件夾中,其中也有我的生成文件。生成文件看起來像:

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

% : %.cpp 
    g++ $(CFLAGS) -o [email protected] $< $(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; 
} 

編輯:我只是試着從示例文件夾本身運行示例代碼,它的工作原理。如果將makefile放入opencv/samples/cpp文件夾中,但make不會在執行復制時將makefile粘貼到其他位置,這樣makefile就可以正常運行。

回答

2

該錯誤表示鏈接程序找不到您的main函數。即使stitch.cpp定義main(我假設),鏈接器找不到它。原因不明,因爲你構建Makefile的方式。我會做這些改變:

  • 使用CXXFLAGS和/或CPPFLAGS用於C++,因爲CFLAGS是C.
  • 擴大化妝的pkg配置輸出,所以你可以看到你的編譯器被要求做。

你的Makefile應該再是這樣的:

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

% : %.cpp 
    g++ $(CXXFLAGS) -o [email protected] $^ $(LIBS) 

我想認爲,這些變化對實際問題的來源將變得明顯。