Reference link: how to configure OpenCV on CLion- Don't know, don't hit the answer. - know https://www.zhihu.com/question/47331502/answer/166576437
Environment: windows 10 professional version 64bit + MinGW 3.22 + cmake 3.8.1 (cmake version should be a newer version, such as this version) + clion 2017.1 (my clion2021.2.3) + opencv3.20 (it can be a newer version, I use 4.5.4 myself)
preparation:
Cmake Download:
https://cmake.org/download/ (download page)
https://cmake.org/files/v3.8/cmake-3.8.1-win64-x64.msi (cmake-3.8.1-win64-x64)
opencv Download:
http://opencv.org/releases.html (download page)
https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/opencv-3.2.0-vc14.exe/download (opencv-win/3.2.0)
Official start:
Suppose you have installed and configured cmake and mingw, and downloaded the file opencv-3.2.0-vc14.exe
Download opencv:
Clicking the windows option will jump to the sourceforge page and wait a few seconds for automatic download
Download it
mingw Download:
It will also jump to sourceforge and download directly
1. Open opencv-3.2.0-vc14.exe and extract the file
After completion:
2. Open CMake and select the source code directory as D:/opencv/sources. After the operation, save the file to D: \ opencv \ MinGW build
Click Configure, and in the pop-up window, select the type as MinGW Makefiles
3. Wait until the configuration is completed and select the relevant entry according to personal needs (for example, python, doc, etc. can be removed). Then click Configure again, and you will find that the red area turns white. At this time, you can click Generate and wait for the operation to be completed.
4. Next, open cmd, enter D: \ opencv \ MinGW build, enter mingw32 make, and wait
I built it three times, passed it the third time, and reported errors the first two times.
Error 1:
D:\opencv\sources\modules\highgui\src\window_w32.cpp: In function 'int icvCreateTrackbar(const char*, const char*, int*, int, CvTrackbarCallback, CvTrackbarCallback2, void*)': D:\opencv\sources\modules\highgui\src\window_w32.cpp:1853:81: error: 'BTNS_AUTOSIZE' was not declared in this scope
The reason for the error is that several variables are not found, because in mingw's header file commctrl.h, the definition of the above variable macro is actually defined by_ WIN32_ Whether ie is defined or not is determined. Let's open the code in D:\MinGW\include\commctrl.h to view:
Amend to read:
Error 2:
Building CXX object modules/ts/CMakeFiles/opencv_ts.dir/src/ts_gtest.cpp.obj D:\opencv\source\opencv\modules\ts\src\ts_gtest.cpp: In constructor 'testing::internal::Mutex::Mutex()': D:\opencv\source\opencv\modules\ts\src\ts_gtest.cpp:8829:45: error: cannot convert 'CRITICAL_SECTION* {aka _RTL_CRITICAL_SECTION*}' to 'GTEST_CRITICAL_SECTION* {aka CRITICAL_SECTION*}' in initialization
stay https://github.com/opencv/opencv/issues/8105 Find the reason in, MinGW definitions_ CRITICAL_ SECTION and _ RTL_ CRITICAL_ SECTION as two separate (equivalent) structs, instead of using typedef.
Modify the file D: \ opencv \ sources \ modules \ TS \ include \ opencv2 \ TS \ TS_ Conditional compilation in GTEST. H. You can view the details https://github.com/opencv/opencv/pull/8123/files
I downloaded TS directly from github_ GTEST. H, covering the local ts_gtest.h
The third build does not report an error. Next, continue to enter mingw32 make install in cmd. This command will generate an install folder.
5. Add D: \ opencv \ MinGW build \ install \ x86 \ MinGW \ bin to the path system environment variable and restart
6. Open Clion and test
First edit CMakeLists.txt
cmake_minimum_required(VERSION 3.7) project(assigment) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # Where to find CMake modules and OpenCV set(OpenCV_DIR "D:\\opencv\\mingw-build\\install") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) add_executable(test_cv main.cpp) # add libs you need set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs) # linking target_link_libraries(test_cv ${OpenCV_LIBS})
Test code:
#include "iostream" #include<opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { Mat img = imread("lena.jpg"); if (img.empty()) { cout << "Error" << endl; return -1; } imshow("Lena", img); waitKey(); return 0; }
The results are shown in the figure
===========================================
reference resources:
[1] use OpenCV with Clion IDE on Windows
[2] OpenCV development environment configuration - Windows/MinGW/Clion/CMake - lhyz - blog Park
[3] Fix for #8105 (compiling with mingw32) by msk-repo01 · Pull Request #8123 · opencv/opencv