Explain cmake find_package opencv cannot be found

Table of contents

Explain cmake find_package opencv cannot be found

Problem Description

problem causes

Solution

Step 1: Confirm OpenCV is installed correctly

Step 2: Configure the installation path of OpenCV

Windows platform

macOS and Linux platforms

Step 3: Rerun CMake

Instructions

working principle

Configuration library dependencies

Summarize


Explain cmake find_package opencv cannot be found

When using CMake to build a project, sometimes the find_package command cannot find the OpenCV library. This article will detail the cause and solution of this problem.

Problem Description

When we use the find_package(OpenCV REQUIRED) command in CMakeLists.txt , we expect CMake to find the installed OpenCV library in the system and add it to the project. However, you may encounter error messages similar to the following when executing CMake:

plaintextCopy code
CMake Error at CMakeLists.txt:10 (find_package):
Could not find a package configuration file provided by "OpenCV" with any of
the following names:
  OpenCVConfig.cmake
  opencv-config.cmake
...

problem causes

This problem is usually caused by:

  1. The OpenCV library is not installed correctly: Before executing find_package , please make sure that you have correctly installed the OpenCV library and can access it through the command line.
  2. The path to the OpenCV library is not configured correctly: CMake cannot find the OpenCV library because you have not configured the OpenCV installation path correctly.

Solution

To resolve this issue, you can take the following steps:

Step 1: Confirm OpenCV is installed correctly

First, make sure you have installed the OpenCV library correctly. You can verify this by running the following command in the terminal:

plaintextCopy code
pkg-config --modversion opencv

If OpenCV was installed correctly, you should be able to see the version number of OpenCV installed.

Step 2: Configure the installation path of OpenCV

For CMake to be able to find the OpenCV library, you need to configure the installation path of OpenCV. This can be achieved by setting the OpenCV_DIR environment variable:

Windows platform

Open a command prompt and run the following command:

plaintextCopy code
setx -m OpenCV_DIR "<path_to_opencv_installation>"

Make sure to replace <path_to_opencv_installation> with your actual installation path.

macOS and Linux platforms

Open a terminal and run the following command (you need to run this command every time you open a new terminal, or add it to your bashrc or zshrc file):

plaintextCopy code
export OpenCV_DIR="<path_to_opencv_installation>"

Make sure to replace <path_to_opencv_installation> with your actual installation path.

Step 3: Rerun CMake

After completing the above steps, re-run CMake. At this point it should be able to correctly find the OpenCV library and add it to the project.

When using CMake to build a project using OpenCV, you can configure it according to the following sample code. First, save the following code as a CMakeLists.txt file:

cmakeCopy code
cmake_minimum_required(VERSION 3.12)
project(OpenCVExample)
# 寻找OpenCV库
find_package(OpenCV REQUIRED)
# 将编译目标指定为可执行文件,并链接OpenCV库
add_executable(opencv_example main.cpp)
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})

Next, create a file called main.cpp and add the following sample code:

cppCopy code
#include <opencv2/opencv.hpp>
#include <iostream>
int main()
{
    // 读取图像文件
    cv::Mat image = cv::imread("image.jpg");
    
    if (image.empty())
    {
        std::cout << "无法读取图像文件!" << std::endl;
        return -1;
    }
    
    // 在窗口中显示图像
    cv::imshow("Image", image);
    cv::waitKey(0);
    
    return 0;
}

After completing the above steps, you can build and run the project as follows:

  1. Create a folder called build in the project root directory and enter that folder.
  2. Execute the cmake .. command to generate the build file.
  3. Execute the make command to compile the project.
  4. Execute the ./opencv_example command to run the executable file.

find_package is a command in CMake used to find, locate and import third-party libraries. It can automatically find the required libraries in the project and configure them as dependencies of the CMake project. This makes using third-party libraries in CMake more convenient and portable.

Instructions

The basic syntax of the find_package command is:

cmakeCopy code
find_package(<Package> [version] [EXACT] [QUIET] [MODULE]
  [REQUIRED] [[COMPONENTS] [components...]]
  [OPTIONAL_COMPONENTS components...]
  [NO_POLICY_SCOPE]
)
  • <Package> : The name of the package to be found, such as OpenCV. There are some common defined package names that can be used in CMake, and you can also search through custom module files.
  • [version] : Optional parameter, specifying the version number of the required library.
  • [EXACT] : Optional parameter, specifying whether the version number needs to match exactly.
  • [QUIET] : Optional parameter, if the package cannot be found, the error message will be suppressed.
  • [MODULE] : Optional parameter, used when searching using custom module files.
  • [REQUIRED] : Optional parameter, indicating that this library is a required dependency of the project. If it is not found, an error will be raised.
  • [[COMPONENTS] [components...]] : Optional parameter that specifies the specific components of the library to be found. For example, if the library has optional submodules, such as OpenCV's opencv_core, opencv_imgproc, etc., you can use this parameter to specify the specific component.
  • [OPTIONAL_COMPONENTS components...] : Optional parameter that specifies the optional components of the library to be found.
  • [NO_POLICY_SCOPE] : Optional parameter, specifying that CMake 3.12 or above is required.

working principle

The find_package command will search for the specified library in the system in a predetermined order. It first searches the system directories and then looks for libraries based on the system's path settings and the location of the module files. If the corresponding package is found, find_package will set some variables for the project so that the library can be used. During the search process, CMake will determine the location of the found library based on some predefined rules, including search files, environment variables, predefined paths, etc.

Configuration library dependencies

It is important to use the find_package command to find and configure library dependencies. Once the required library is found, CMake sets the corresponding variables (such as <PackageName>_FOUND , <PackageName>_INCLUDE_DIRS , <PackageName>_LIBRARIES, etc.), which can be used in the project to link the library and include header files. For example, after using the find_package(OpenCV REQUIRED) command to find and configure the OpenCV library dependencies, you can use the ${OpenCV_INCLUDE_DIRS} variable to include the OpenCV header file, and use the ${OpenCV_LIBRARIES} variable to link to the OpenCV library.

find_package is an important command in CMake for finding, locating and importing third-party libraries. It makes using third-party libraries in CMake projects more convenient and portable by automating the search and configuration of library dependencies. Understanding and correctly using the find_package command can help us handle and manage project dependencies.

Summarize

The inability to find the OpenCV library is caused by the installation path of OpenCV not being configured correctly. This issue can be resolved by confirming that OpenCV is installed correctly and setting the correct OpenCV_DIR environment variable.

Guess you like

Origin blog.csdn.net/q7w8e9r4/article/details/135401454