tags: Cmake find_package linux
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 configuration files in some directories.
2. After finding it,find_packageWill set the header file directory to${OpenCV_INCLUDE_DIRS}In, set the link library to${OpenCV_LIBS}in.
3. Set the link library and header file directory of the executable file and compile the file.
Therefore, our CMakeLists can be written like this:
add_executable(Main main.cpp)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(Main ${OpenCV_LIBS})
After understanding the usage, the natural problem is: How does the find_package command find the Opencv configuration file?
Two modes: module mode and configuration mode. Regardless of the mode, you want to find a *.cmake file.
Module mode means: first variable${CMAKE_MODULE_PATH}Start looking in all the directories in the directory, if not, then look at its own module directory /share/cmake-x.y/Modules/ ).This is called module mode.
The configuration mode means: look up each package directory in ~/.cmake/packages/ or /usr/local/share/, look for <capitalization of the library name>Config.cmake or <lowercase of the library name>-config.cmake (For example, the library Opencv, it will look for OpenCVConfig.cmake or opencv-config.cmake in /usr/local/share/OpenCV),This is called the configuration mode.
After finding *.cmake, the following variables are generally defined in cmake:
<NAME>_FOUND
<NAME>_INCLUDE_DIRS or <NAME>_INCLUDES
<NAME>_LIBRARIES or <NAME>_LIBRARIES or <NAME>_LIBS
<NAME>_DEFINITIONS
Pay attention to the above,Especially the word or. Therefore, in CmakeLists, "include_directories(${OpenCV_INCLUDE_DIRS})" should be replaced by "include_directories(${OpenCV_INCLUDES})". When linking libraries to executables, you can also change OpenCV_LIBS to OpenCV_LIBRARIES. and many more.
Regarding this piece of content, I personally think that it is enough to understand this. If you want to further investigate the details, some friends have given a better explanation, so I do not need to repeat or copy, directly list the address:
find_packageIt can be used to find two modes of third -party module, Find_package, one isModuleMode, another is calledConfigmodel (1)ModuleIn the mode, CMAKE needs to find one called Find<LibraryNa...
Official Documentation: cmake-modules(7) — Ccmake-modules(7) — C 1. System predefined modules You can view the above link, if there is a CURL, then find_package(CURL) 2. Non-...
Commands in CMakefind_package is used to find the specified package。 find_package supports two main search methods:Note: <PackageName> is case sensitive ...
background In many cases, compiling a project requires linking to a third-party library. In this case, you need to know the following information about the third-party library: .h header file address ...
The main contents of this article are as follows: 1. The basic principle of cmake find_package 2. How to write your own cmake module 3. Use cmake find_package to use different versions of opencv lib (...
find_package()Principle interpretation According to the official cmake documentation,find_package()There are Module mode (basic signature) and Config mode (full signature, full usage). The Module mode...
Find_package can't find a package, you can add a sentence in front....
CMAKE: Learn notes, contains some instruction function functions Introduction to CMAKE CMake is a derivative produced by Kitware and some open source developers in the development of several toolkits ...
1. Introduction to the Find_Package module Official reference documentation:https://cmake.org/cmake/help/latest/command/find_package.html Find external projects and import their setup information 2, b...
CMAKE command find_package uses notes Article catalog CMAKE command find_package uses notes Command format Use example Search mode Module mode Config mode Search path Windows Search Directory Linux Se...