【OpenCV CMake find_packages找不到包】

根据OpenCV和各大网站提供的基于CMake的find_packages方法总是提示如下两种错误。
错误1:找不到OpenCV

CMake Error at CMakeLists.txt:39 (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.

缺少一步,需要用set配置OpenCV_DIR变量,例如set(OpenCV_DIR /xx/opencv-4.8.0/install/sdk/native/jni/abi-arm64-v8a)路径和abi根据自己的需要修改,该目录下包含了OpenCVConfig.cmake文件定义了各种导入需要的变量。

但是在配置了OpenCV_DIR仍有可能遇到如下错误

[ 50%] Building CXX object CMakeFiles/example.dir/main.cpp.o
[100%] Linking CXX executable example
ld: error: unable to find library -lopencv_core
ld: error: unable to find library -lopencv_dnn
ld: error: unable to find library -lopencv_imgcodecs
ld: error: unable to find library -lopencv_imgproc
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/example.dir/build.make:107: example] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/example.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

这是因为在CMake编译OpenCV时开启了BUILD_opencv_world选项,该选项开启后,OpenCV会生成一个静态库,在install/sdk/native/staticlibs目录下,它包含了包括所有模块,而不是每个模块一组单独的二进制文件。关闭该选项或者在CMake导入时,不再使用target_link_libraries(example PUBLIC ${OpenCV_LIBS}),改为target_link_libraries(example PUBLIC opencv_world)都可以解决该问题。
OpenCV的windows cmake安装教程里有说明

你可能感兴趣的:(opencv,webpack,javascript)