我有一个将OpenCV添加到项目的CMake。
我使用以下代码将openCV添加到项目中:
if (MSVC)
file(TO_CMAKE_PATH $ENV{OPENCV_ROOT} OpenCV_DIR)
IF(NOT OpenCV_DIR)
MESSAGE( FATAL_ERROR "Please point the environment variable OpenCV_ROOT to the root directory of OpenCV installation. required openCv V 4.2.x as minimum")
ENDIF()
set(BUILD_SHARED_LIBS OFF)
find_package(OpenCV 4.2.0 REQUIRED)
else (MSVC)
set(BUILD_SHARED_LIBS ON)
find_package(OpenCV COMPONENTS core highgui imgproc imgcodecs videoio photo stitching flann ml features2d calib3d objdetect REQUIRED)
endif(MSVC)
如果我没有定义OpenCV_root环境变量,则CMake找不到OpenCV,如果定义了它,则会收到以下警告:
CMake Warning (dev) at CMakeLists.txt:36 (find_package):
Policy CMP0074 is not set: find_package uses <PackageName>_ROOT variables.
Run "cmake --help-policy CMP0074" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
Environment variable OpenCV_ROOT is set to:
D:\local\opencv
For compatibility, CMake is ignoring the variable.
This warning is for project developers. Use -Wno-dev to suppress it.
那么,如果我没有定义openCV_root环境变量,CMake应该如何找到OpenCV?
对于其他软件包(例如boost,我也得到同样的警告),同样的问题也是有效的,我们是否应该定义一个env变量?
如果我没有定义env变量,OpenCV将如何找到该软件包?
最佳答案
CMP0074策略的description告诉:
In CMake 3.12 and above the
find_package(<PackageName>)
command now searches prefixes specified by the<PackageName>_ROOT
CMake variable and the<PackageName>_ROOT
environment variable... This policy provides compatibility with projects that have not been updated to avoid using<PackageName>_ROOT
variables for other purposes.
也就是说,在您的项目中,您需要消除使用的
OPENCV_ROOT
变量,而不是直接使用而不影响find_package(OpenCV)
行为的来达到目的。在较新的CMake中,此变量自动使用 :
# This sets CMP0074 to NEW.
cmake_minimum_required(VERSION 3.12) # Or bigger version
set(BUILD_SHARED_LIBS OFF)
# If `OPENCV_ROOT` variable is set, it will be used in the next call without a warning.
find_package(OpenCV 4.2.0 REQUIRED)
如果您希望项目对安装OpenCV有其他提示,请使用其他名称的变量。
# We want to support old CMake versions too!
cmake_minimum_required(VERSION 3.11) # Or lower version
# Use `OPENCV_INSTALL_PREFIX` environment variable for set `OpenCV_DIR`, which helps CMake to find OpenCV.
# This setting works for both new and old CMake versions.
file(TO_CMAKE_PATH $ENV{OPENCV_INSTALL_PREFIX} OpenCV_DIR)
IF(NOT OpenCV_DIR)
MESSAGE( FATAL_ERROR "Please point the environment variable OPENCV_INSTALL_PREFIX to the root directory of OpenCV installation. required openCv V 4.2.x as minimum")
ENDIF()
set(BUILD_SHARED_LIBS OFF)
find_package(OpenCV 4.2.0 REQUIRED)
或者,您可以禁用CMP0074策略,并根据需要使用OPENCV_ROOT变量。但这是,不建议使用:
# 'find_package' won't use `_ROOT` variable. This suppress the corresponding warning.
cmake_policy(SET CMP0074 OLD)
file(TO_CMAKE_PATH $ENV{OPENCV_ROOT} OpenCV_DIR)
IF(NOT OpenCV_DIR)
MESSAGE( FATAL_ERROR "Please point the environment variable OpenCV_ROOT to the root directory of OpenCV installation. required openCv V 4.2.x as minimum")
ENDIF()
set(BUILD_SHARED_LIBS OFF)
find_package(OpenCV 4.2.0 REQUIRED)
关于c++ - CMake如何查找软件包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60453531/