Opencv4 使用 pkg-config 给出未定义的引用错误
Opencv4 giving undefined reference errors with pkg-config
我最近更新到了ubuntu 20.04。默认的OpenCV版本现在是4.2,它给出了很多编译错误。
下面是一个示例程序
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char const *argv[]) {
Mat src = imread(argv[1], 1);
namedWindow("src", 1);
imshow("src", src);
// Split the image into different channels
vector<Mat> rgbChannels(3);
split(src, rgbChannels);
// Show individual channels
Mat g;
g = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
// Showing Red Channel
// G and B channels are kept as zero matrix for visual perception
{
Mat img_R;
vector<Mat> channels;
channels.push_back(g);
channels.push_back(g);
channels.push_back(rgbChannels[2]);
vector<Mat> R = channels;
/// Merge the three channels
merge(R, img_R);
namedWindow("R", 1);
imshow("R", img_R);
namedWindow("R2", 1);
imshow("R2", rgbChannels[2]);
}
// Showing Green Channel
{
Mat img_G;
vector<Mat> channels;
channels.push_back(g);
channels.push_back(rgbChannels[1]);
channels.push_back(g);
vector<Mat> G = channels;
merge(G, img_G);
namedWindow("G", 1);
imshow("G", img_G);
namedWindow("G2", 1);
imshow("G2", rgbChannels[1]);
}
// Showing Blue Channel
{
Mat img_B;
vector<Mat> channels;
channels.push_back(rgbChannels[0]);
channels.push_back(g);
channels.push_back(g);
vector<Mat> B = channels;
merge(channels, img_B);
namedWindow("B", 1);
imshow("B", img_B);
namedWindow("B2", 1);
imshow("B2", rgbChannels[0]);
}
// Showing Red Blue Channel
{
Mat img_RB;
vector<Mat> channels;
channels.push_back(rgbChannels[0]);
channels.push_back(g);
channels.push_back(rgbChannels[2]);
vector<Mat> RB = channels;
merge(RB, img_RB);
namedWindow("RB", 1);
imshow("RB", img_RB);
}
waitKey(0);
return 0;
}
现在当我使用
g++ -g -I/usr/include/opencv4 `pkg-config --libs --cflags opencv4` rgb.cpp
然后我得到这些引用错误
/usr/bin/ld: /tmp/cc1tVO10.o: in function `main':
/home/gaurav-pant/lab/img_process/rgb.cpp:10: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:12: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:13: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:17: undefined reference to `cv::split(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:21: undefined reference to `cv::Mat::zeros(cv::Size_<int>, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:35: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:36: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:37: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:38: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:39: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:52: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:53: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:54: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:55: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:56: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:70: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:71: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:72: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:73: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:74: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:87: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:88: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:89: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:92: undefined reference to `cv::waitKey(int)'
/usr/bin/ld: /tmp/cc1tVO10.o: in function `cv::Mat::Mat(cv::Mat const&)':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:538: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/usr/bin/ld: /tmp/cc1tVO10.o: in function `cv::Mat::~Mat()':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:739: undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/cc1tVO10.o: in function `cv::Mat::release()':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:851: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
巧合的是,当我使用 CMake 包含库时,此类错误不存在。我该如何克服这个问题?
库排序在这里很重要。正确的编译命令是
g++ -g rgb.cpp `pkg-config --libs --cflags opencv4`
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++引用错误
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 只有级联分类器会发出未定义的引用错误
- 2个模板化类的非模板友元函数未定义引用错误
- C/C++ 取消引用错误:在空检查之前取消引用
- 为什么我的C++代码无法编译,出现未定义的引用错误
- 我的代码中C++未定义的引用错误?
- 模板专用化会导致未定义的引用错误
- .cpp文件中未定义的引用错误
- c++ 中目录中文件的未定义引用错误
- Opencv4 使用 pkg-config 给出未定义的引用错误
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- 共享指针取消引用错误
- 编译简单的多文件类程序时出现未定义的引用错误
- 模板函数产生未定义的引用错误
- 调用提取重加载器会产生对"运算符"的未定义引用错误>>
- 如何修复"未定义的引用"错误 opencv 和 g++
- 未定义的引用错误,我该怎么办?