在Cmakelist中添加opencv遇到的问题

    xiaoxiao2023-10-10  170

    问题

    编译一个开源项目,该项目的Cmakelist:

    find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) if(NOT OpenCV_FOUND) find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc) endif()

    cmake后出错:

    CMake Error at examples/CMakeLists.txt:5 (find_package): By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "OpenCV", but CMake did not find one. Could not find a package configuration file provided by "OpenCV" with any of the following names: OpenCVConfig.cmake opencv-config.cmake Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files. If "OpenCV" provides a separate development package or SDK, be sure it has been installed.

    最终解决方案

    其实 cmakelist想做的无非是通过find_package()来添加opencv,find_package是根据系统的环境变量来找到需要的库的,出现了以上问题是我们没有设置好opencv的各种路径。这里我通过更直接的方法来添加opencv,修改cmakelist:

    set(OpenCV_DIR C:/software/opencv/build) #注意修改成自己的路径,该路径包含OpenCVConfig.cmake、opencv-config.cmake这两个文件 include_directories(${OpenCV_DIR}/include) add_library(opencv STATIC IMPORTED ) set_target_properties(opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/x64/vc14/lib/opencv_world330.lib) #这个路径也要设置正确

    (未)解决过程

    回到最初的报错信息,意思是要添加一个包含OpenCVConfig.cmake、opencv-config.cmake的路径,提示可以通过set(OpenCV_DIR),修改cmakelist:

    set(OpenCV_DIR C:/software/opencv/build) #保证该路径下有OpenCVConfig.cmake、opencv-config.cmake文件 find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) if(NOT OpenCV_FOUND) find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc) endif()

    再cmake,再报错:

    CMake Warning at C:/software/opencv/build/OpenCVConfig.cmake:138 (message): Found OpenCV Windows Pack but it has no binaries compatible with your configuration. You should manually point CMake variable OpenCV_DIR to your build of OpenCV library. Call Stack (most recent call first): examples/CMakeLists.txt:5 (find_package) CMake Error at examples/CMakeLists.txt:5 (find_package): Found package configuration file: C:/software/opencv/build/OpenCVConfig.cmake but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be NOT FOUND.

    前面的Warning的意思是少了路径指向Opencv的lib,提示我们手动用OpenCV_DIR 指向lib,后面的Error是说OpenCVConfig.cmake把OpenCV_FOUND设置成了False,而我们的cmakelist是需要OpenCV_FOUND为True(原因和前面是一样的,因为没有找到lib导致False), 再次修改cmakelist:

    set(OpenCV_DIR C:/software/opencv/build\ C:/software/opencv/build/x64/vc14/lib) #保证该路径下有OpenCVConfig.cmake、opencv-config.cmake文件 find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) set(OpenCV_FOUND 1) if(NOT OpenCV_FOUND) find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc) endif()

    这时cmake无报错,但make会报错找不到opencv相关的头文件,把头文件路径加入cmakelist:

    set(OpenCV_DIR C:/software/opencv/build\ C:/software/opencv/build/x64/vc14/lib) #保证该路径下有OpenCVConfig.cmake、opencv-config.cmake文件 find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) set(OpenCV_FOUND 1) if(NOT OpenCV_FOUND) find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc) endif() include_directories(C:/software/opencv/build/include) #添加头文件目录

    再cmake,再make,报错:

    LINK: command "C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1411~1.255\bin\HostX64\x64\link.exe /nologo @CMakeFiles\squeezenet.dir\objects1.rsp /out:squeezenet.exe /implib:squeezenet.lib /pdb:E:\ncnn-master\build-vs2017\examples\squeezenet.pdb /version:0.0 /machine:x64 /INCREMENTAL:NO /subsystem:console ..\src\ncnn.lib opencv.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:squeezenet.exe.manifest" failed (exit code 1181) with the following output: LINK : fatal error LNK1181: 无法打开输入文件“opencv.lib” NMAKE : fatal error U1077: “E:\pcw\cmake-3.10.0-rc1-win64-x64\bin\cmake.exe”: 返回代码“0xffffffff” Stop. NMAKE : fatal error U1077:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64\nmake.exe": 返回代码“0x2” Stop. NMAKE : fatal error U1077:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64\nmake.exe": 返回代码“0x2” Stop.

    提示“无法打开opencv.lib”,我猜是因为添加的库名字的opencv,所以编译时会试图打开opencv.lib文件,但其实opencv在/x64/vc14/lib路径下的lib名字是opencv_worldxxx.lib,就是说我并没有正确地设置好lib的路径,因此问题仍未解决。 最终考虑到无非是一个添加链接库的问题,我就使用了前面所说的方法解决,希望大家能帮我指出后面这种方法的问题所在!

    最新回复(0)