簡體   English   中英

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`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM