Issue
I installed OpenCV using Anaconda, with the following command.
mamba create -n opencv -c conda-forge opencv matplotlib
I know that the installation is fully functional because the below works:
import cv2
c = cv2.imread("microphone.png")
cv2.imwrite("microphone.jpg",c)
import os
os.getpid() # returns 13249
Now I try to do the same using C++.
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
using namespace cv;
int main()
{
std::string image_path = "microphone.png";
Mat img = imread(image_path, IMREAD_COLOR);
if(img.empty())
{
std::cout << "Could not read the image: " << image_path << std::endl;
return 1;
}
imwrite("microphone.JPG", img);
return 0;
}
And the compilation:
> g++ --version
g++ (conda-forge gcc 12.3.0-3) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
...
> export PKG_CONFIG_PATH=/home/stetstet/mambaforge/envs/opencv/lib/pkgconfig
> g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`
When I run the above, g++ complains that I am missing an OpenGL.
/home/stetstet/mambaforge/envs/opencv/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/bin/ld: warning: libGL.so.1, needed by /home/stetstet/mambaforge/envs/opencv/lib/libQt5Widgets.so.5, not found (try using -rpath or -rpath-link)
After some experimentation I discover that some of the libraries must be from /usr/lib/x86_64-linux-gnu
, while others must be used from /home/stetstet/mambaforge/envs/opencv/lib/
(opencv
is the name of the venv in use). The following yields an a.out
which does what was intended:
> /usr/bin/g++ opencv_test.cpp `pkg-config --cflags --libs opencv4` -lpthread -lrt
The /usr/bin/g++
so that it can actually find libGL.so.1
as well as libglapi.so.0
, libselinux.so.1
, libXdamage.so.1
, and libXxf86vm.so.1
. Also, without -lpthread -lrt
these libraries are used from the venv, which causes "undefined reference to `h_errno@GLIBC_PRIVATE'"
Now, I am very bothered by the fact that I now need to know which one of which library (and g++/ld) I should use. I thought package managers were supposed to handle the dependency mess for us!
Would there be any way to make the compilation command into something like
> g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`
i.e. have all relevant files or binaries inside the venv? For example, is there a way to modify the mamba create
command (see top) so that this condition is satisfied?
Note: I am tagging both Anaconda, Linux, and OpenCV because I have absolutely no idea what I can use to reach a solution.
Solution
You need pkg-config
to also be in the env. The following should work:
mamba create -n opencv -c conda-forge opencv matplotlib pkg-config
mamba activate opencv
g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`
Answered By - MEE Answer Checked By - David Marino (WPSolving Volunteer)