'CLion and CMake Doesn't Complain but Failed to build OpenCV

Summary

In Windows, CLion doesn't complain anything after cmake and before makefile.

  • All code seems to link correctly without error. I am able to see the reference, documents, linter and jump into cv::Mat or opencv.hpp header file with ctrl RMB.
  • CMake seems to correctly generate make files without error.

But the compile error occurs: undefined reference to OpenCV methods.

my setup

CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)

project(test)

# Set compile version
set(CMAKE_CXX_STANDARD 17)
add_executable(test ./test.cpp )
find_package(OpenCV REQUIRED)

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    libraries: ${OpenCV_LIBRARIES}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

target_link_libraries(test ${OpenCV_LIBRARIES})

The output seems correct

"D:\Program Files\JetBrains\CLion 2021.3.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\repo\CS5330\test
-- OpenCV library status:
--     version: 4.5.5
--     libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
--     libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
--     include path: /path/to/scoop/apps/opencv/current/include
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/test/cmake-build-debug

[Finished]

And this generates CMakeCache.txt, which includes,

//The directory containing a CMake configuration file for OpenCV.
OpenCV_DIR:PATH= /path/to/scoop/apps/opencv/current/x64/vc15/lib

//Details about finding OpenCV
FIND_PACKAGE_MESSAGE_DETAILS_OpenCV:INTERNAL=[/path/to/scoop/apps/opencv/current][v4.5.5()]

As you see, system-wide environment variable OpenCV_DIR has been already set and read correctly by CLion.

Here is a simple test code, but failed to run

#include <opencv2/opencv.hpp>
int main() {
    cv::Mat img = cv::imread("./test.jpg", -1);
    cv::imshow("Mon image", img);
    cv::waitKey(0);
    return 0;
}

I also installed msys2 from winget, and from msys2 installed clang, make, MinGW-w64 GDB,cmake. Following this tutorial. And tested through that toolchain instead of CLion bundled toolchain, it returns the same result.

OpenCV binary is from scoop.

For some reason, it has the same problem as one from homebrew. In opencv.hpp, includes headers are incorrect due to file hierarchy that opencv.hpp is inside opencv2 folder. I changed all #include "opencv2/header.hpp" to #include "header.hpp", but it doesn't help, and vice versa.

Can't figure out the reason for hours.. Any help will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source