Cmake entry (four) find_package

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:

cmake tutorial 4 (use of find_package)

Intelligent Recommendation

Cmake learning Find_package

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...

CMake understands find_package in depth

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-...

Use of find_package in CMake

Commands in CMakefind_package is used to find the specified package。       find_package supports two main search methods:Note: <PackageName> is case sensitive      ...

6.CMake find_package use summary

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 ...

cmake tutorial 4 (use of find_package)

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 (...

More Recommendation

In-depth understanding of CMake: the use of find_package()

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...

CMAKE Configuration Find_package Specified Path

Find_package can't find a package, you can add a sentence in front....

CMAKE Common Command - Set (), Find_Package (), ...

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 ...

CMAKE Learning Notes 06 - Find_Package

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

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top