The following briefly introduces how Cmake uses the find_package command to find external libraries:
cmake itself does not provide any convenient method for searching the library, nor does it set the environment variables of the library itself. It just searches for the Findxxx.cmake file and the xxxConfig.cmake file in the specified search path in the order of priority (where xxx represents the name of the library, and special attention is paid to the case). These two files are generally not The difference is that cmake can find any of these two files, and we can successfully use the library, that is, we can use the built-in Cmake variables of the library. Contains the path information of the header file and library file of the library. Although the author of the library generally provides these two files, it may not be found after installation. When we are in the cmake.. command, Cmake will read and execute the code in CMakeLists.txt, when the find_package() command is executed, Cmake will find the Findxxx.cmake file or xxxConfig.cmake file from some path , 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 variables (NAME represents the name of the library, 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, the commonly used ones are xxx_FOUND, xxx_INCLUDE_DIRS, xxx_LIBS, which represent the flag of whether the library is found, the path of the header file, and the path of the library file respectively. find_package() has two modes: Module mode and Config mode, which correspond to the above Findxxx.cmake and xxxConfig.cmake files respectively. cmake gives priority to Module mode by default, and Config mode is an alternative.
Module mode (just find Findxxx.cmake file):
Cmake will first search the path specified by CMAKE_MODULE_PATH. If CMAKE_MODULE_PATH is not set in CMakeLists.txt as the path to store Findxxx.cmake, that is to say, there is no command below:
set(CMAKE_MODULE_PATH "The path where the Findxxx.cmake file is located")
Then Cmake will not search the path specified by CMAKE_MODULE_PATH. At this time, Cmake will search for the second priority path, which is <CMAKE_ROOT>/share/cmake-xy/Mdodules (Note: xy means Version number. Mine is 3.10). CMAKE_ROOT is the system path when you installed Cmake. Because I did not specify the installation path, it is the default path of the system. In my system (ubuntu16.04), the default path of the system is /usr/loacl, if you Used in the installation process
cmake -DCMAKE_INSTALL_PREFIX=Your own dir path, then CMAKE_ROOT represents the path you wrote. Just said that the first priority path search does not find the Findxxx.cmake file, it will search under the second priority path. If Cmake does not find the Findxxx.cmake file in either path. Then Cmake will enter Config mode.
Config mode (just find the xxxConfig.cmake file):
Cmake will first search the path specified by xxx_DIR. If this cmake variable is not set in CMakeLists.txt. That is to say, there is no following command:
set(xxx_DIR "The path where the xxxConfig.cmkae file is located")
Then Cmake will not search the path specified by xxx_DIR, at this time Cmake will automatically search under the second priority path, which is /usr/local/lib/cmake/xxx/ XxxConfig.cmake file in.
The above mainly talks about the search mode of Cmake. If Cmake does not find the corresponding Findxxx.cmake and xxxConfig.cmake files in the paths provided by the two modes, the system will prompt the top error messages.
Now take the but_velodyne package search as an example:
find_package(but_velodyne REQUIRED)
Now find: /usr/local/lib/cmake/but_velodyne-0.1/but_velodyne-config.cmake
cat but_velodyne-config.cmake:
# Users can set the following variables before calling the module:
# ButVELODYNE_DIR - The preferred installation prefix for searching for ButVELODYNE. Set by the user.
#
# ButVELODYNE_ROOT_DIR - the root directory where the installation can be found
# ButVELODYNE_CXX_FLAGS - extra flags for compilation
# ButVELODYNE_LINK_FLAGS - extra flags for linking
# ButVELODYNE_INCLUDE_DIRS - include directories
# ButVELODYNE_LIBRARY_DIRS - link directories
# ButVELODYNE_LIBRARIES - libraries to link plugins with
# ButVELODYNE_Boost_VERSION - the boost version but_velodyne was compiled with
get_filename_component(_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_PREFIX "${_PREFIX}" PATH)
get_filename_component(_PREFIX "${_PREFIX}" PATH)
get_filename_component(ButVELODYNE_ROOT_DIR "${_PREFIX}" PATH)
set( ButVELODYNE_CXX_FLAGS "-DBUT_VELODYNE_DLL " )
set( ButVELODYNE_LINK_FLAGS "" )
set( ButVELODYNE_INCLUDE_DIRS "${ButVELODYNE_ROOT_DIR}/include/but_velodyne-0.1")
set( ButVELODYNE_LIBRARY_DIRS "${ButVELODYNE_ROOT_DIR}/lib")
set( ButVELODYNE_LIBRARIES but_velodyne${ButVELODYNE_LIBRARY_SUFFIX})
set( ButVELODYNE_Boost_VERSION "1.58")
mark_as_advanced(
ButVELODYNE_ROOT_DIR
ButVELODYNE_CXXFLAGS
ButVELODYNE_CXX_FLAGS
ButVELODYNE_LINK_FLAGS
ButVELODYNE_INCLUDE_DIRS
ButVELODYNE_LIBRARIES
ButVELODYNE_Boost_VERSION
)
How to view the result of find_package():
find_package(but_velodyne REQUIRED)
if (but_velodyne_FOUND)
MESSAGE (STATUS "@@@@@@dern: ${ButVELODYNE_DEFINITIONS}")
MESSAGE (STATUS "@@@@@@dern: ${ButVELODYNE_INCLUDE_DIRS}")
MESSAGE (STATUS "@@@@@@dern: ${ButVELODYNE_LIBRARY_DIRS}")
else()
MESSAGE (STATUS "@@@@@@dern: but_velodyne not found")
endif(but_velodyne_FOUND)
Output:

OpenCV


There will be a share folder in the file installed in opt, which contains the OpenCV folder we need, so if you want to have a choice of control version, you should put it before the sentence find_package Go to set where the OpenCV folder is located, and add the sentence set(OpenCV_DIR /opt/opencv-2.4.11/share/OpenCV). This can be changed according to the location of your install.
————————————————
Copyright statement: This article is the original article of the CSDN blogger "AndyCheng_hgcc". It follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/chengde6896383/article/details/86497016
Basic grammar rules Internal and external construction Example 7 External build and project and message instructions Basic grammar rules From the previous examples, we used several basic commands of c...
content 1, problem description 2. Problem 3, solution 4, DEMO verification 1, problem description In the ARM environment, look for the Protobuf library with protobuf_generate_cpp, use protobuf_generat...
Chapter 5 of Mastering_CMake creates its own package and uses find_package in other projects to find its own created package ide is clion, gcc environment is MinGW Project directory and files 1. Creat...
Find_package is a very important instruction in Cmake.Simple to use, powerful。 Let's first introduce its operation process, taking the most used opencv as an example: 1.find_packageLook for OpenCV con...
1、 find_package(<Name>)Command first looks for the module pathFind<name>.cmakeThis is a typical way to find the library. Find a specific path followed by CMake: variable${CMAKE_MODULE...
Reprinted and summarized fromIn-depth understanding of CMake (3): the use of find_package() CMake learning log find_package First, first clarify the default search path Second, understand the two mode...
cmake find_package opencv not found #find opencv lib find_package(OpenCV REQUIRED NO_MODULE # should be optional, tells CMake to use conf...
referenceCMAKE official documentation find_package()There are two usage:Basic Signature and Module ModewithFull Signature and Config Mode。 in front of CMAKEModulesFind under the directoryFind<Packa...
One problem that is often encountered when cmakelists.txt compiles the corresponding package, such as an error is as follows: Our own cmakelists.txt writtenfind_package(catkin REQUIRED COMPONENTS ,[RE...
CMAKELISTS.TXT The result is not found to find Torch. Let Find_Package toSpecify pathSearch, there are three ways: Set DIR Set Torch_Dir in cmakelists.txt Set PATHS CMAKELISTS.TXT modification Specify...