简体   繁体   English

如何在CMake中知道诸如'OpenCV'之类的变量

[英]How to know variable such as 'OpenCV' in CMake

I am using OpenCV with gcc and cmake. 我正在使用OpenCV与gcc和cmake。 And I found a tutorial https://docs.opencv.org/3.4.0/db/df5/tutorial_linux_gcc_cmake.html .In the file CMakeLists.txt , there are some variables such as OpenCV and OpenCV_INCLUDE_DIRS . 我找到了一个教程https://docs.opencv.org/3.4.0/db/df5/tutorial_linux_gcc_cmake.html 。在文件CMakeLists.txt ,有一些变量,如OpenCVOpenCV_INCLUDE_DIRS

cmake_minimum_required(VERSION 3.9)
project(VideoRecord)

set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(VideoRecord main.cpp)
target_link_libraries(VideoRecord ${OpenCV_LIBS})

I want to know where to find these variables definition. 我想知道在哪里找到这些变量的定义。

EDIT 编辑

Thanks @qbranchmaster's answer. 谢谢@ qbranchmaster的回答。 I tried to search FindOpenCV.cmake but failed. 我试图搜索FindOpenCV.cmake但失败了。

First try. 第一次尝试。

➜  ~ cmake --help-module-list | grep "FindOpen"
FindOpenACC
FindOpenAL
FindOpenCL
FindOpenGL
FindOpenMP
FindOpenSSL
FindOpenSceneGraph
FindOpenThreads

Another try. 另一个尝试。

➜  / find . "FindOpenCV.cmake"

In addition, my os is osx and I install cmake with brew . 另外,我的os是osx,我用brew安装cmake I comiple and install OpenCV manually. 我手动编写并安装OpenCV

These variables are part of the package config script shipping with OpenCV. 这些变量是随OpenCV一起提供的包配置脚本的一部分。

Note that find_package is a two-headed beast. 请注意, find_package是一个双头野兽。 The classic mode of operation is finding libraries through find-scripts . 经典的操作模式是通过查找脚本查找库。 This is still the approach being used today for third-party libraries that are not aware of CMake. 这仍然是今天用于不了解CMake的第三方库的方法。 However, if your dependency is itself being built with CMake, it can provide a package config file instead, which allows for a more powerful mode of operation. 但是,如果您的依赖项本身是使用CMake构建的,那么它可以提供包配置文件 ,这允许更强大的操作模式。

The idea here is that instead of you telling CMake how to find a dependency, the dependency itself tells CMake how clients can find it. 这里的想法是,不依赖于告诉CMake如何找到依赖关系,依赖关系本身告诉CMake客户端如何找到它。 This is the approach that is taken by libraries like OpenCV and Qt. 这是OpenCV和Qt等图书馆采用的方法。

To answer your question, those variables are being set by the package config file in your local OpenCV installation, the template of which can be found in the OpenCV source code under cmake/templates/OpenCVConfig.cmake.in . 要回答您的问题,这些变量由本地OpenCV安装中的软件包配置文件设置,其模板可以cmake/templates/OpenCVConfig.cmake.in的OpenCV源代码中 cmake/templates/OpenCVConfig.cmake.in

They are defined in CMake OpenCV module. 它们在CMake OpenCV模块中定义。 CMake has numerous modules that aid in finding various libraries like OpenCV (FindOpenCV.cmake module). CMake有许多模块可以帮助找到像OpenCV(FindOpenCV.cmake模块)这样的各种库。 Using this command you can get a list of modules that your CMake supports: 使用此命令,您可以获得CMake支持的模块列表:

cmake --help-module-list

Some libraries come with their own *.cmake modules which should be installed in some system path. 有些库带有自己的* .cmake模块,应该安装在某些系统路径中。 If you are using Ubuntu, your cmake modules should be localised in: 如果您使用的是Ubuntu,那么您的cmake模块应该本地化为:

 /usr/share/cmake/Modules/

If not, just search system for file FindOpenCV.cmake. 如果没有,只需在系统中搜索FindOpenCV.cmake文件。 In that file you will find these variables. 在该文件中,您将找到这些变量。

In general, you get variable names from the documentation or source code of the package you want to find. 通常,您从要查找的包的文档或源代码中获取变量名称。

Often you can derive the name to put into find_package from the provided FindFoo.cmake module file name, because "Foo" would be the name. 通常,您可以从提供的FindFoo.cmake模块文件名派生名称以放入find_package ,因为“Foo”将是名称。 The find module is either part of CMake or comes with the third-party library. find模块可以是CMake的一部分,也可以与第三方库一起提供。
If there is no find module, some modules provide FooConfig.cmake files, where "Foo" is again the string to put into find_package . 如果没有find模块,一些模块提供FooConfig.cmake文件,其中“Foo”再次是放入find_package的字符串。
If you have neither a find nor a config file, you need to find the library by other means, eg, FindPkgConfig or find_library / find_file . 如果既没有查找文件也没有配置文件,则需要通过其他方式查找库,例如FindPkgConfig或find_library / find_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM