Keywords: OpenCV | pkg-config | Ubuntu installation
Abstract: This article provides an in-depth analysis of the common error 'Package opencv was not found in the pkg-config search path' encountered after installing OpenCV on Ubuntu systems. It begins by explaining the root cause: pkg-config's inability to locate the opencv.pc file. The traditional manual method of creating this file and setting environment variables is discussed, highlighting its limitations. The focus then shifts to the recommended automated installation script maintained by the community, which streamlines dependency management and configuration. Additional solutions, such as using apt-file for package search and adjustments for OpenCV 4.0, are included as alternatives. By comparing these approaches, the article offers comprehensive guidance for efficiently setting up an OpenCV development environment, ensuring robustness and ease of use.
Background and Root Cause Analysis
On Ubuntu or its derivative Linux distributions, a frequent issue arises after installing OpenCV: executing the command pkg-config --cflags --libs opencv returns the error message "Package opencv was not found in the pkg-config search path." This error stems from pkg-config's failure to locate the OpenCV package configuration file, opencv.pc, within its default search paths. pkg-config is a tool for managing compilation and linking flags, relying on .pc files to provide metadata such as include paths and library lists. If opencv.pc is not properly generated or placed during OpenCV installation, this issue occurs.
Traditional Solution: Manual Configuration of opencv.pc
Early users often addressed this problem manually. First, create a file named opencv.pc with content specifying OpenCV's installation paths and library information. For example, a typical opencv.pc file might contain:
prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
Copy this file to the /usr/local/lib/pkgconfig directory, then modify the .bashrc file to add environment variables:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
After running source ~/.bashrc to apply changes, pkg-config can correctly identify the OpenCV package. However, this method requires manual file creation, is error-prone, and difficult to maintain, especially with OpenCV version updates.
Recommended Solution: Using Automated Installation Scripts
With community advancements, a more efficient solution is available. It is recommended to use automated installation scripts from GitHub, such as opencv_latest.sh downloaded from this link. This script, maintained by the community, automates OpenCV compilation, installation, and environment configuration. Steps include:
- Download the script: Use wget or curl to obtain
opencv_latest.sh. - Grant execution permissions: Run
chmod +x opencv_latest.sh. - Execute the script: Run
./opencv_latest.sh; it will automatically install dependencies, compile OpenCV, and configure pkg-config paths.
This approach avoids manual configuration hassles, ensures proper generation and placement of opencv.pc, and resolves pkg-config issues in one step. It is ideal for beginners or scenarios requiring quick setup, and supports the latest OpenCV versions.
Alternative Solutions and Considerations
Beyond the above methods, other options are available. First, use the command apt-file search opencv.pc to search for the opencv.pc file in the system, aiding in diagnosis. If related development packages are missing, running sudo apt-get install libopencv-dev might automatically resolve pkg-config issues, as this package typically includes necessary configuration files.
For OpenCV 4.0 and later, note two points: add the -DOPENCV_GENERATE_PKGCONFIG=ON parameter during compilation to ensure opencv.pc generation; and use pkg-config --cflags --libs opencv4 instead of opencv, as the package name may have changed. These adjustments reflect changes in OpenCV version evolution, and users should adapt based on their installed version.
Conclusion and Best Practices
In summary, multiple approaches exist to resolve pkg-config's failure to find the OpenCV package. Manual configuration suits advanced users or specific custom needs, but automated scripts are recommended for their convenience and reliability. Users should prioritize automated scripts, as they incorporate community best practices and reduce human error. During implementation, ensure basic build tools are installed, e.g., via sudo apt-get install build-essential. Additionally, regularly check OpenCV official documentation and community updates for the latest installation guides to avoid compatibility issues. Through this analysis, developers can efficiently set up a stable OpenCV development environment and focus on implementing computer vision projects.