pkg-config can't find opencv
Asked Answered
A

4

13

I installed opencv on a lubuntu 12.10 distro. Then when I try to compile a code which is using opencv it says it can't find it. So I try in a terminal :

pkg-config --cflags --libs opencv

It answers me that it can't find opencv. But the files are installed in /usr/lib. I don't understand why it can't find them.

Armoury answered 27/2, 2013 at 13:50 Comment(4)
You need to have the package libopencv-dev installed.Constancy
I had only installed libcv, I added libopencv and it worked. Thanks a lot.Armoury
It seems you have found your answer. Could you please answer your own question and accept it.Pretender
Installing that package solved my isssueParole
A
11

You have to put pkg-config --cflags --libs opencv at the end of your g++ line. For example :

g++ test.cpp -o test `pkg-config --cflags --libs opencv`

Compiles

g++ `pkg-config --cflags --libs opencv` test.cpp -o test

Doesn't compile and have undefined reference.

Atavistic answered 16/1, 2014 at 17:42 Comment(0)
P
15

For OpenCV 4 you might have to use:

pkg-config --cflags --libs opencv4

(Note the 4 in the end!)

Pyrrhic answered 18/9, 2020 at 11:54 Comment(0)
A
11

You have to put pkg-config --cflags --libs opencv at the end of your g++ line. For example :

g++ test.cpp -o test `pkg-config --cflags --libs opencv`

Compiles

g++ `pkg-config --cflags --libs opencv` test.cpp -o test

Doesn't compile and have undefined reference.

Atavistic answered 16/1, 2014 at 17:42 Comment(0)
G
2

From OpenCV 4:

add -DOPENCV_GENERATE_PKGCONFIG=YES to cmake build arguments.

Use YES, ON is not working anymore.

Gesner answered 12/12, 2022 at 19:48 Comment(0)
M
-1

Many good answers (and comments). Adding a comprehensive one that (hopefully) addresses the entire problem.
Also, working on updated environment (at answer time): Ubuntu 22 (with OpenCV4).

According to [FreeDesktop]: pkg-config:

pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line so an application can use gcc -o test test.c pkg-config --libs --cflags glib-2.0 for instance, rather than hard-coding values on where to find glib (or other libraries).

Example:

[cfati@cfati-5510-0:/mnt/e/Work/Dev/StackExchange/StackOverflow/q015113753]> . ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> pkg-config --version
0.29.2
[064bit prompt]> 
[064bit prompt]> # Search for (older) OpenCV
[064bit prompt]> pkg-config --modversion opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
[064bit prompt]> 
[064bit prompt]> # Search for OpenCV4
[064bit prompt]> pkg-config --modversion opencv4
4.5.4

But how does Pkg-Config determine that information (and how come OpenCV4 works while OpenCV doesn't)?
The answer is simple: it reads (and displays information from) .pc files. More details can be found at:

Worth mentioning that it's similar to CMake's Find*.cmake files.

So, Pkg-Config searches for (.pc) files in a list of default folders. More can be added by specifying them in PKG_CONFIG_PATH.

[064bit prompt]> # Default search paths (each on separate line) for .pc files
[064bit prompt]> pkg-config --variable pc_path pkg-config | tr ":" "\n"
/usr/local/lib/x86_64-linux-gnu/pkgconfig
/usr/local/lib/pkgconfig
/usr/local/share/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/pkgconfig
/usr/share/pkgconfig

Back to OpenCV(4):

[064bit prompt]> pkg-config --debug --modversion opencv4 2>&1 | grep "Reading 'opencv4"
Reading 'opencv4' from file '/usr/lib/x86_64-linux-gnu/pkgconfig/opencv4.pc'

And the actual package owning the file:

[064bit prompt]> dpkg -S /usr/lib/x86_64-linux-gnu/pkgconfig/opencv4.pc
libopencv-dev: /usr/lib/x86_64-linux-gnu/pkgconfig/opencv4.pc

So, in order for OpenCV4 to be Pkg-Config-able, installing the OpenCV .deb (and dependents) is not enough, the development package LibOpenCV-Dev (libopencv-dev) is required.

[064bit prompt]> apt list | grep libopencv-dev

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libopencv-dev/jammy,now 4.5.4+dfsg-9ubuntu4 amd64 [installed]

Example making use of it.

main00.cpp:

#include <opencv2/core/types.hpp>

#include <stdio.h>


int main()
{
    cv::Mat mat(100, 100, CV_8UC4, cv::Scalar(0x00, 0x00, 0x00, 0x00));
    printf("\nDone.\n\n");
    return 0;
}

Output:

[064bit prompt]> ls
main00.cpp
[064bit prompt]>
[064bit prompt]> # ---------- WRONG ORDER ----------
[064bit prompt]> g++ $(pkg-config --cflags --libs opencv4) main00.cpp -o main00
/usr/bin/ld: /tmp/cc79oIaV.o: in function `main':
main00.cpp:(.text+0x67): undefined reference to `cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)'
/usr/bin/ld: main00.cpp:(.text+0x87): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: main00.cpp:(.text+0xad): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
[064bit prompt]>
[064bit prompt]> # ---------- RIGHT ORDER ----------
[064bit prompt]> g++ main00.cpp $(pkg-config --cflags --libs opencv4) -o main00
[064bit prompt]> ls
main00  main00.cpp
[064bit prompt]> ldd main00
    linux-vdso.so.1 (0x00007ffdeb2c6000)
    libopencv_core.so.4.5d => /lib/x86_64-linux-gnu/libopencv_core.so.4.5d (0x00007f7551e00000)
    libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7551a00000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7552188000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7551600000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f7551de4000)
    libtbb.so.2 => /lib/x86_64-linux-gnu/libtbb.so.2 (0x00007f7551d9e000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7551cb7000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f75521d2000)
[064bit prompt]>
[064bit prompt]> ./main00

Done.

The ordering difference (fail / success) is due to the fact that the GNU Linker resolves symbols (in passed objects and libraries) from left to right.
Leaving the source file (and its corresponding object (.o) file) at the end, leaves it with unresolved symbols (in spite of the libraries containing those symbols being passed before it - and thus already searched). For more details, check [SO]: Why does the order in which libraries are linked sometimes cause errors in GCC?.
So this part has nothing to do with Pkg-Config, OpenCV, or any other package as a matter of fact, it's a generic GNU Linker feature.

Music answered 29/10, 2023 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.