Ask Your Question
0

Opencv4 giving undefined reference errors with pkg-config

asked 2020-05-12 06:18:05 -0500

gauravpant gravatar image

I have recently updated to ubuntu 20.04. The default OpenCV version is now 4.2 and it is giving a lot of compilation errors.

Below is a sample program

#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;
}

Now when I compile using

g++ -g -I/usr/include/opencv4 `pkg-config --libs --cflags opencv4` rgb.cpp

Then I get these reference errors

/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 ...
(more)
edit retag flag offensive close merge delete

Comments

try to avoid using pkg-config in favour of using cmake or a simple, handwritten cmdline or makefile.

also, just curious, what does pkg-config --libs --cflags opencv4 resolve to ? (if you just run it on a cmdline)

berak gravatar imageberak ( 2020-05-12 06:22:20 -0500 )edit
1

Yeah, I am using CMake only now, but pkg-config worked perfectly fine on previous versions and since I had to use OpenCV for compiling single cpp file, it was more convenient.

This is what I get when I run pkg-config --libs --cflags opencv4

-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_dnn -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core

gauravpant gravatar imagegauravpant ( 2020-05-12 06:40:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-12 07:33:26 -0500

I don't think you should be giving the header files' directory explicitly (the -I/usr/include/opencv4 option), because pkg-config does that for you. The --libs part of pkg-config supplies the .so files, and --cflags part supplies the .h an .hpp files.

In summary, use this command:

g++ -g `pkg-config --libs --cflags opencv4` rgb.cpp

You might want to locate the opencv4.pc file in your filesystem (find / -type f -regex '^.*opencv4.pc$'), and inspect it's contents (cat /path/to/opencv4.pc), to better understand what I explained in the first paragraph.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-05-12 06:16:18 -0500

Seen: 116 times

Last updated: May 12