Linux系统配置opencv(GPU)时出现,find_package(OpenCV REQUIRED)的编译错误
在安装opcv-GPU版本时,在cmakelist文件的配置问题出错.导致编译失败。
具体报错警告如下:
问题一:
CMake Error at /usr/local/lib/cmake/opencv4/OpenCVConfig.cmake:86 (find_package):
By not providing "FindCUDA.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "CUDA", but
CMake did not find one.
Could not find a package configuration file provided by "CUDA" (requested
version 11.8) with any of the following names:
CUDAConfig.cmake
cuda-config.cmake
Add the installation prefix of "CUDA" to CMAKE_PREFIX_PATH or set
"CUDA_DIR" to a directory containing one of the above files. If "CUDA"
provides a separate development package or SDK, be sure it has been
installed.
问题一解决在CMake Error : OpenCV CUDA CMakeLists.txt - CUDA Setup and Installation - NVIDIA Developer Forums
问题一解析:
由于在CMake 3.27版本中不再需要使用该模块或调用find_package(CUDA)
来编译CUDA代码,为了解决我最初提出的问题,我简单地将cmake_minimum_required()
降低到3.27版之前的任何版本,例如3.26或更低。
问题一解决:
cmake_minimum_required(VERSION 3.26) 降低指定cmake版本
cmake_minimum_required(VERSION 3.26)
问题二:
解决问题一后,问题二出现,编译器报错如下:
CMake Warning (dev) at /usr/local/lib/cmake/opencv4/OpenCVConfig.cmake:86 (find_package):
Policy CMP0146 is not set: The FindCUDA module is removed. Run "cmake
--help-policy CMP0146" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
问题二解析:
- 这个警告是因为在CMake中,FindCUDA模块已经被移除。要解决这个问题,你需要设置CMP0146策略并使用cmake_policy命令来抑制这个警告。你可以在你的CMakeLists.txt文件中添加以下代码:
问题解决:
if(POLICY CMP0146) cmake_policy(SET CMP0146 OLD) endif()
if(POLICY CMP0146)
cmake_policy(SET CMP0146 OLD)
endif()
最后编译成功通过,可以运行opencv项目代码