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",);
//namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
//imshow("MyWindow", img);
//waitKey(0);
cv::Mat locMask(img.rows,img.cols,CV_8UC1,cv::Scalar());
//imshow("locMask",locMask);
while (infile >> img_filename >> head_num) {
for(int i = ; 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=;i<coordinates.size();i++)
cout<<coordinates[i]<<" ";
drawContours(locMask,coordinates,-,cv::Scalar::all(), CV_FILLED);
imwrite("mask01.jpg",locMask);
imshow("locMask",locMask);
waitKey();
return ;
}

我的文件截图:

版权声明:本文为博主原创文章,欢迎转载,转载请注明原文地址、作者信息。

Opencv undefined reference to `cv::imread() Ubuntu编译的更多相关文章

  1. 「caffe编译bug」.build_release/lib/libcaffe.so: undefined reference to cv::imread

    转自:https://www.douban.com/note/568788483/ CXX/LD -o .build_release/tools/convert_imageset.bin.build_ ...

  2. Opencv3.0: undefined reference to cv::imread(cv::String const&, int)

    使用opencv,编译出错: undefined reference to cv::imread(cv::String const&, int) 自opencv3.0之后,图像读取相关代码在i ...

  3. test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)'

    opencv报错: test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)' te ...

  4. caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.

    when I compile caffe file : .build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::Str ...

  5. undefined reference to cv::imread(cv::String const&, int)

    .build_release/lib/libcaffe-nv.so: undefined reference to cv::imread(cv::String const&, int)' .b ...

  6. error: undefined reference to `cv::imread(std::string const&, int)'

    g++ main.cpp -o main `pkg-config --libs --cflags opencv`注意--libs的位置https://stackoverflow.com/questio ...

  7. openCV中 libopencv-nonfree-dev的安装: undefined reference to `cv::initModule_nonfree()&#39;

    今天照着一起做RGB-D SLAM (3)    , 程序会出现以下的错误: cv::initModule_nonfree(); /home/yhzhao/slam/src/detectFeature ...

  8. .build_release/lib/libcaffe.so: undefined reference to `cv::VideoCapture::set(int, double)'

    CXX/LD -o .build_release/tools/convert_imageset.bin.build_release/lib/libcaffe.so: undefined referen ...

  9. undefined reference to `cv::VideoCapture

    出现opencv链接的问题原因: 1. 路径设置不正确,caffe会优先搜索Makefile.config里面的环境设置 2. anaconda2装的opencv和配置的opencv路径不一致 比如, ...

随机推荐

  1. sublime lincense for linux

    sublime lincense for linux Sublime Text 3.x (after Build 3133) —– BEGIN LICENSE —–TwitterInc200 User ...

  2. React高级教程(es6)——(1)JSX语法深入理解

    从根本上来说,JSX语法提供了一种创建React元素的语法糖,JSX语句可以编译成: React.createElement(component, props, …children)的形式,比如: & ...

  3. 对比PG数据库结构是否一致的方法

    如果版本升级涉及数据库更新,测试时要对比旧版数据库更新后是否和全新安装的是否一致,但是数据库结构很复杂的时候对比就很麻烦,postgre可以使用以下方法对比:pg_dump.exe –h 数据库IP ...

  4. opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓

    一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...

  5. mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法

    mysql数据库连接出问题,提示超时,后来发现问题在于连接mysql数据库的jar包跟数据库版本不对应导致的,更换jar包一致就解决了.

  6. 不错的redis文章

    参考: https://www.cnblogs.com/itdragon/tag/redis/

  7. 【转】Rancher 2.0 里程碑版本:支持添加自定义节点!

    原文链接: http://mp.weixin.qq.com/s?__biz=MzIyMTUwMDMyOQ==&mid=2247487533&idx=1&sn=c70258577 ...

  8. ES6标准之基础

    let和const命令 ES6新增let命令,用于声明变量,是块级作用域. let声明的变量不会像var声明的变量发生“变量提升”现象,所以,变量一定要在声明后使用,不然就会报错. 暂时性死区:只要块 ...

  9. ECharts注释

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  10. 20175317 《Java程序设计》第五周学习总结

    20175317 <Java程序设计>第五周学习总结 教材学习内容总结 第五周我学习了教材第六章的内容,了解了接口的知识,学到了以下内容: 明白了什么是接口 学会了如何实现接口 了解了接口 ...