Ubuontu16.04 process and use find_package () error messages caused by installing Opencv library, ubuontu16.04opencv

Source: Internet
Author: User

Ubuontu16.04 process and use find_package () error messages caused by installing Opencv library, ubuontu16.04opencv

After installing the Opencv library, test whether the Opencv library is successfully installed. The following example shows the corresponding. cpp code and the corresponding cmakelists.txt code:

. Cpp file:

 1 #include <stdio.h> 2 #include <opencv2/opencv.hpp> 3 using namespace cv; 4 int main(int argc, char** argv ) 5 { 6   if ( argc != 2 ) 7   { 8     printf("usage: DisplayImage.out <Image_Path>\n"); 9     return -1;10   }11   Mat image;12   image = imread( argv[1], 1 );13   if ( !image.data )14   {15     printf("No image data \n");16     return -1;17   }18   namedWindow("Display Image", WINDOW_AUTOSIZE );19   imshow("Display Image", image);20   waitKey(0);21   return 0;22 }

CMakeLists.txt file:

1 cmake_minimum_required(VERSION 2.8)2 project(DisplayImage)3 find_package( Opencv  REQUIRED)4 if(Opencv_FOUND)5     message(STATUS "The Opecv lib is found!") 6 endif()7 add_executable( Display test.cpp)8 arget_link_libraries( Display ${OpenCV_LIBS} ) 

Create and enter the build directory under the project directory, and then enter the command: cmake ..
The following error message is displayed:

CMake Error at CMakeLists.txt:5 (find_package):  By not providing "FindOpencv.cmake" in CMAKE_MODULE_PATH this project has  asked CMake to find a package configuration file provided by "Opencv", but  CMake did not find one.  Could not find a package configuration file provided by "Opencv" with any  of the following names:    OpencvConfig.cmake    opencv-config.cmake  Add the installation prefix of "Opencv" to CMAKE_PREFIX_PATH or set  "Opencv_DIR" to a directory containing one of the above files.  If "Opencv"  provides a separate development package or SDK, be sure it has been  installed.

According to its prompt, it is found that the FindOpencv. cmake file is not found in cmake, and then try to find OpencvConfig. cmake. The result is not found. This is our breakthrough in solving the problem. First, I tried to search FindOpencv. cmake in the root directory of the system, and I also tried to find OpencvCongfig. cmake. None of the results were found. In fact, the author of the library will provide these two files, but I will compile and install the Opencv library according to the installation instructions on the Opencv official website. Still not found. The specific reason is unclear. So I found OpenCVConfig. cmake In the build directory of the compiled Opecnv file and found the OpenCVConfig. cmake file in the/usr/local/share/OpenCV directory. Although similar files have been found, there is still no substantive solution to the problem. So I searched the internet for the use of the find_package () command. The following describes how to use the find_package command of Cmake to search for external libraries. Finally, we propose a solution to solve the above cmake... problems.
Introduction to find_package:
First, it is clear that cmake does not provide any convenient methods for Searching databases, nor does it set the environment variables of libraries. It only searches for Findxxx in the specified search path in the order of priority. cmake file and xxxConfig. cmake file (xxx indicates the name of the database, especially the case sensitive file). The two files are basically the same, and cmake can find any one of the two files, we can use this library successfully, that is, we can use the built-in Cmake variable of the library. It contains the path information of the library header file and library file. Although the library author generally provides these two files, it may not be found after installation. When we are in cmake .. after the command is executed, Cmake reads and executes the code in cmakelists.txt. After the find_package () command is executed, Cmake will find the Findxxx from some paths. cmake file or xxxConfig. in the cmake file, Cmake will execute this file after finding any one, and then some Cmake variables will be set after this file is executed. For example, the following variable (NAME indicates the library NAME. For example, Opencv can be used to represent the Opencv Library ):

<NAME>_FOUND<NAME>_INCLUDE_DIRS or <NAME>_INCLUDES<NAME>_LIBRARIES or <NAME>_LIBRARIES or <NAME>_LIBS<NAME>_DEFINITIONS

Generally, xxx_FOUND, xxx_INCLUDE_DIRS, and xxx_LIBS are commonly used to indicate whether the library logo, library header file path, and library file path are found. Find_package () has two modes: Module mode and Config mode, which correspond to the above Findxxx. cmake and xxxConfig. cmake files respectively. By default, cmake takes precedence over the Module mode, while Config mode takes precedence over the backup mode.

Module mode (only find the Findxxx. cmake file ):
Cmakewill give priority to the search criteria specified by cmake_module_path. if cmakelists.txt does not set CMAKE_MODULE_PATH as the path for storing Findxxx. cmake, there is no command below:
Set (CMAKE_MODULE_PATH "path of Findxxx. cmake file ")
Then Cmake will not search for the path specified by CMAKE_MODULE_PATH, Cmake will search for the second-priority path, that is, <CMAKE_ROOT>/share/cmake-x.y/Mdodules (Note: x. y indicates the version number. My account is 3.10 ). Here, CMAKE_ROOT is the system path when you install Cmake. Because I have not specified the installation path, it is the default system path. In my system (ubuntu16.04) the default path of the system is/usr/loacl. If you use
Cmake-DCMAKE_INSTALL_PREFIX = Your dir path, then CMAKE_ROOT indicates the path you write. If the Findxxx. cmake file is not found in the first-priority path search, the file is searched in the second-priority path. If the Findxxx. Cmake file is not found in both paths. Then Cmake enters the Config mode.

Config mode (only search for the xxxConfig. cmake file ):
Cmake will first search for the path specified by xxx_DIR. If the cmake variable is not set in cmakelists.txt. That is to say, there is no following command:
Set (xxx_DIR "xxxConfig. cmkae file path ")
Then Cmake will not search for the path specified by xxx_DIR. Then Cmake will automatically search for the path with the second priority, that is, xxxConfig in/usr/local/lib/cmake/xxx. cmake file.
The above describes the Cmake search mode. If the Findxxx. Cmake and xxxConfig. cmake files are not found in the paths provided by the two modes, the system will prompt the top error messages.

Problem Analysis:
Review the preceding error message. The dimensions file does not include the command for setting CMKAE_MODULE_PATH and Opencv_DIR. That is to say, after Cmake executes find_package, it will automatically search for Findxxx in the two default second-priority search paths. cmake and xxxConfig. cmake file. Although I installed the system directory by default when installing Opencv, I found no FindOpencv In the second priority search path <CMAKE_ROOT>/share/cmake-x.y/Mdodules in Moudule mode. cmake file. Then, I found no Opencv folder in the second-priority search path/usr/loacl/lib/cmake/in Config mode, and did not find the OpencvConfig. cmake file. Instead, I only saw the Pangolin folder that I installed on the system. The PangolinConfig. cmake file does exist. However, I found the OpenCV. cmake file in the/usr/local/share/OpenCV/folder. But it is not the OpencvConfig. cmake file mentioned in the above error message. Only the case is different.

Solution:
(1 ),We can ignore the error message above, because we use find_package () to find the library, that is, to use include_directories () to include the header file. Use link_directories () to include library files. Finally, use target_link_libraries (Executable File Library) to link dynamic or dynamic libraries. The simplest way is to find the header file path and library file path in the directory of the Opencv package. You can set the following command in cmakelists.txt:

1 set (Opencv_INCLUDE_DIRS "Opencv library header file directory") 2 set (Opencv_LIBRARIES_DIRS "Opencv library file directory") 3 set (Opencv_LIBs "specific link library file. a. so ") 4 include_directories ($ {Opencv_INCLUDE_DIRS}) 5 link_directories ($ {Opencv_LIBRARIES_DIRS}) 6 target_link_libraries (<execution File Name>" $ {Opencv_LIBs })

A bad thing here is the "" part of the command. The first two commands are fine. We only need to write down the specific file path. For the third command. There are a lot of files in the specific link library, and we may not write them completely. The good thing is that we didn't use so many libraries, so simple programs may compile and run normally, however, when the program is complex, the required database is not something we can consider. We must add all possible databases here, if the library file or header file required by the project is missing, compilation will fail. This is a fatal drawback. Of course, in my own case, my installation package is installed in the system directory by default. That is to say, I can just set the next specific link library file without adding the header file and library file path. so. the file set ending with. If you install it elsewhere, you need to set the path containing the library file and header file.
There are two options for the path "Opencv library header file directory. One is the include directory under the installation package, and the other is/usr/local/include
The path "library file directory of Opencv library" has two options. One is the build/bin of the installation package, and the other is/usr/local/lib.
The path "specific link library file" has two options. One is the build/bin/Name of all the library files of the installation package, and the other is the name of all the library files under/usr/local/lib (note: I still don't know how to write this "Specific Link Library File". I only know how to write all the files. If anyone knows how to simplify writing, leave a message for me)

(2 ),The first method avoids the above error information, but it also needs to do a lot of work in the future. If the job in each step is not in place, compilation may fail. The following method is to find the FindOpencv. cmake and OpencvConfig. cmake files according to the information shown above. As discussed in the problem analysis, the default search path of my cmake does not contain any of the above files. Only the Opencv. cmake file is found in the OpenCV installation package and/usr/local/share/OpenCV paths. The following operations can be selected in four ways: (My opencv is installed in the system directory, so the cmakelists.txt does not contain two commands including header files and library files .) :
1. Let the system search in Module mode, that is, find a FindOpencv. cmake file, and then set the CMAKE_MODULE_PATH variable. The specific operation is as follows: The FindOpencv. cmake file is not found in all directories. Only the OpenCVConfig. cmake file can be found. We can change the file name to FindOpencv. cmake (there are many ways to change the file name. If you do not have the permission, you can right-click it and change it. If you have the permission, you must change it to sudo in the command line ). Add the CMAKE_MODULE_PATH to cmakelists.txt. Because the OpenCVConfig. cmake file appears in two locations, one is the build/directory of the package, and the other is/usr/local/share/OpenCV. I chose to change it by right-clicking under the build/of the package. After modification, add the following command before the find_package command in cmakelists.txt:
Set (CMAKE_MODULE_PATH/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build)
The command in the last cmakelists.txt is as follows:

1 cmake_minimum_required(VERSION 2.8)2 project(DisplayImage)3 set(CMAKE_MODULE_PATH  /home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build)4 find_package( Opencv  REQUIRED)5 if(Opencv_FOUND)6    message(STATUS "The Opecv lib is found!") 7 endif()8 add_executable( Display test.cpp)9 target_link_libraries( Display ${OpenCV_LIBS} )

2. Let the system search in Config mode, find a way to get an OpencvConfig. cmake file, and then set the Opencv_DIR variable (Opencv_DIR must not be OpenCV_DIR ). The specific operation is as follows: The OpencvConfig. cmake file is not found in all directories. Only the OpenCVConfig. cmake file can be found. We can change the file name to OpencvConfig. cmake (refer to the above for the specific change method ). Add the Opencv_DIR path information in cmakelists.txt. Because the OpenCVConfig. cmake file appears in two locations, one is the build/directory of the package, and the other is/usr/local/share/OpenCV. I chose to change it by right-clicking under the build/of the package. After modification, add the following command before the find_package command in cmakelists.txt:
Set (Opencv_DIR/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build)
The command in the last cmakelists.txt is as follows:

1 cmake_minimum_required(VERSION 2.8)2 project(DisplayImage)3 set(Opencv_DIR  /home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build)4 find_package( Opencv  REQUIRED)5 if(Opencv_FOUND)6    message(STATUS "The Opecv lib is found!") 7 endif()8 add_executable( Display test.cpp)9 target_link_libraries( Display ${OpenCV_LIBS} )

3. Let the system search by Module mode and set OpenCVConfig. change the cmake file to FindOpenCV. cmake, set the CMAKE_MODULE_PATH, and specify a name for find_package, for example, find_package (Opencv NAMES OpenCV REQUIRED ). In this case, the find_package will find the FindOpenCV. cmake file. The specific operation is as follows: The FindOpencv. cmake file is not found in all directories. Only the OpenCVConfig. cmake file can be found. You can change the file name to findopencv.cmake. then, add the CMAKE_MODULE_PATH path to cmakelists.txt. Because the OpenCVConfig. cmake file appears in two locations, one is the build/directory of the package, and the other is/usr/local/share/OpenCV. I chose to change it by right-clicking under the build/of the package. After modification, add the following command before the find_package command in cmakelists.txt:
Set (CMAKE_MODULE_PATH/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build)
And modify the find_package content. The specific command is as follows:
Find_package (Opencv NAMES OpenCV REQUIRED)
The command in the last cmakelists.txt is as follows:

1 Runtime (VERSION 2.8) 2 project (DisplayImage) 3 set (CMAKE_MODULE_PATH/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build) 4 find_package (Opencv namopencv REQUIRED) # Alternatively, use find_package (OpenCV REQUIRED) 5 if (Opencv_FOUND) 6 message (STATUS "The Opecv lib is found! ") 7 endif () 8 add_executable (Display test. cpp) 9 target_link_libraries (Display $ {OpenCV_LIBS })

4. Let the system search in Config mode, set the Opecv_DIR path, and specify a name when find_package is used, for example, find_package (Opencv NAMES OpenCV REQUIRED ). Or the output indicates that the Config mode is used.
Find_package (Opencv configs names OpenCV REQUIRED), because OpenCVConfig. the cmake file appears at two locations: one is the build/directory of the package, and the other is/usr/local/share/OpenCV. I chose to set Opencv_DIR In the build/path of the package. After modification, add the following command before the find_package command in cmakelists.txt:
Set (Opencv_DIR/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build)
And modify the find_package content. The specific command is as follows:
Find_package (Opencv NAMES OpenCV REQUIRED)
The command in the last cmakelists.txt is as follows:

1 cmake_minimum_required (VERSION 2.8) 2 project (DisplayImage) 3 set (Opencv_DIR/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build) 4 find_package (Opencv NAMES OpenCV REQUIRED) # Alternatively, use find_package (OpenCV REQUIRED) 5 if (Opencv_FOUND) 6 message (STATUS "The Opecv lib is found! ") 7 endif () 8 add_executable (Display test. cpp) 9 target_link_libraries (Display $ {OpenCV_LIBS })

Summary:
You can choose either of the above two methods or any of the second methods flexibly. Any method. In fact, there are a lot of details in the above process. You can try them one by one to see what the output content is. For more information about how to use find_package, see the cmake official manual. The following section compares the four vertices of cmakelists.txt with each other (the OpenCVCongfig. cmake file is changed accordingly in the above four methods by default ):

1 cmake_minimum_required (VERSION 2.8) 2 project (DisplayImage) 3 4 # Set the Module mode path (if you want to use it, you must have FindOpencv. cmake or FindOpenCV. cmake, and then select the find_package mode, for example, FindOpencv. cmake corresponds to find_package (Opencv REQUIRED) and FindOpenCV. cmake corresponds to find_package (Opencv NAMES OpenCV REQUIRED) 5 # set (CMAKE_MODULE_PATH/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build) 6 # Set the Config mode path (OpencvConfig is required if you want to use it. cmak E or OpenCVConfig. cmake and then select the find_package mode. The relationship is the same as that in Moudule mode. Note that we can use the Config mode directly, that is, Cmake does not search in Module mode. You need to call find_package (Opencv configs names OpenCV REQUIRED) or find_package (Opencv configs required) 7 set (Opencv_DIR/home/gcj/Slam_Start/slam_directory/slam_packages/opencv-3.4.0/build) 8 9 # corresponding to FindOpenCV. cmake and OpenCVConfig. cmake10 find_package (Opencv NAMES OpenCV REQUIRED) # Or use find_package (OpenCV REQUIRED) 11 12 # To correspond to FindOpencv. camke and OpencvConfig. cmake13 find_package (Opencv REQUIRED) 14 15 if (Opencv_FOUND) 16 message (STATUS "The Opecv lib is found! ") 17 endif () 18 19 # The following two commands containing the header file path can be selectively added. Because I installed the file in the system directory by default, the system can search for the header file by default even if the following command is not added. If you install this library elsewhere, the following sentence is required. 20 # Add path 21 # include_directories ($ {OpenCV_INCLUCE_DIRS}) 22 23 # Add the executable file 24 add_executable (Display test. cpp) 25 26 # the command to link the library file is required. In particular, $ {OpenCV_LIBS} is the cmake variable defined after the xxxConfig. cmake or Findxxx. cmake file is found in cmake and executed. Cmake file. Note that OpenCV is capitalized, because only the OpenCV_LIBS variable is defined in the. cmake file. Therefore, when writing data to other databases, check whether the data is in upper or lower case. You can view the data in the. cmake file. 27 target_link_libraries (Display $ {OpenCV_LIBS })

The above understanding may not be comprehensive. For further understanding, refer to the following references. If you have a better understanding or an error occurs during the above explanation, I hope you can point it out. Thank you!

References:
1. http://blog.csdn.net/bytxl/article/details/50637277#t0
2. https://cmake.org/cmake/help/v3.10/command/find_package.html
3. https://stackoverflow.com/questions/8711109/could-not-find-module-findopencv-cmake-error-in-configuration-process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.