Ubuntu下編譯一個C++文件,C++源程序中使用了opencv,opencv的安裝沒有問題,但是在編譯的過程中出現如下錯誤:
undefined reference to `cv::imread(std::string const&, int)'
undefined reference to `cv::noArray()'
undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
undefined reference to `cv::waitKey(int)'
...
undefined reference to `cv::Mat::deallocate()'
...
網上查了下資料,大概說opencv的靜態編譯庫沒有鏈接到本程序中(也不知道對不對,望大家指正),於是找啊找啊,終於找到一個有用的,記錄一下:
在terminal下運行命令: g++ getmask.cpp -o getmask `pkg-config opencv --cflags --libs` // 包含、鏈接參數一定要放在后面,其實就是在編譯C++程序后面加上(`pkg-config opencv --cflags --libs`)
編譯成功后生成getmask可執行文件,接着運行命令:./getmask 就能得到結果啦。
附上我的源程序:
#include <iostream> #include <vector> #include <fstream> #include <string> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { string img_filename; int head_num; int posx,posy; cv::Point pt; vector<Point> coordinate; vector<vector<Point> > coordinates; ifstream infile("scene01.txt"); Mat img = imread("scene01.jpg",1); //namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //imshow("MyWindow", img); //waitKey(0); cv::Mat locMask(img.rows,img.cols,CV_8UC1,cv::Scalar(0)); //imshow("locMask",locMask); while (infile >> img_filename >> head_num) { for(int i = 0 ; i< head_num;i++) { infile >> posx >> posy; pt = Point(posx,posy); coordinate.push_back(pt); } coordinates.push_back(coordinate); } //cout<<coordinate.size()<<endl; //cout<<coordinates.size()<<endl; for(int i=0;i<coordinates.size();i++) cout<<coordinates[i]<<" "; drawContours(locMask,coordinates,-1,cv::Scalar::all(255), CV_FILLED); imwrite("mask01.jpg",locMask); imshow("locMask",locMask); waitKey(0); return 0; }
我的文件截圖:
版權聲明:本文為博主原創文章,歡迎轉載,轉載請注明原文地址、作者信息。