[OpenCV CMake find_packages cannot find the package]

According to the CMake-based find_packages method provided by OpenCV and major websites, it always prompts the following two errors.
Error 1: OpenCV not found

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.

There is a missing step. You need to use set to configure the OpenCV_DIR variable. For exampleset(OpenCV_DIR /xx/opencv-4.8.0/install/sdk/native/jni/abi-arm64-v8a)The path and abi are modified according to your own needs. This directory contains the OpenCVConfig.cmake file that defines various import needs. variable.

However, you may still encounter the following error after configuring 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

This is because the BUILD_opencv_world option is turned on when CMake compiles OpenCV. After this option is turned on, OpenCV will generate a static library, which contains all modules in the install/sdk/native/staticlibs directory. Rather than a separate set of binaries per module. Turning off this option or no longer using target_link_libraries(example PUBLIC ${OpenCV_LIBS}) when importing with CMake and changing it to target_link_libraries(example PUBLIC opencv_world) can solve this problem.
OpenCV windows cmake installation tutorialInstructions

Guess you like

Origin blog.csdn.net/weixin_42474261/article/details/132322079