Opencv4 使用 pkg-config 给出未定义的引用错误

Opencv4 giving undefined reference errors with pkg-config

本文关键字:引用 错误 未定义 使用 pkg-config Opencv4      更新时间:2023-10-16

我最近更新到了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`