C/C++ development, build OpenCV+MinGW compilation environment under win

Table of contents

1. Source code download

Two, source code compilation

         2.1 cmake-gui configuration compilation options

         2.2 Dynamic library compilation

         2.3 Static library compilation (optional)

   3. Case test

        3.1 Engineering design and configuration

        3.2 Dynamic use test

        3.3 Static library usage test


1. Source code download

        opencv can download the source code on the official website ( Courses - OpenCV ), github, gitee to compile the library with specific functions you need, or you can get the compiled library from SourceForge and apply it directly.

        gitee镜像:opencv: Open Source Computer Vision Library

        It is recommended to use a domestic mirror: git clone [email protected]:mirrors/opencv.git

        Cmake download: Index of /files , download the required version by yourself, it is recommended to be 2.8 or above, directly download the compiled installation version for win, and skip the installation process.

        For the installation and deployment of MinGW+gcc, see: https://pyfree.blog.csdn.net/article/details/129844783

Two, source code compilation

        2.1 cmake-gui configuration compilation options

        Enter the cmake installation directory bin, start the cmake-GUI interface

         In the Cmake GUI management interface, configure the compiled source (src) and compiled output directory (bulid):

         Click the configure button to enter the compilation tool selection interface. My computer has multiple compilation tools installed. In order to prevent the cmake configuration from being cut into other compilers randomly, select the second item to manually specify the local compiler path:

        Click next to enter the c/c++ compilation tool path configuration interface, and select the installed MinGW-gcc/g++ compilation path:

         Click the Finish button to complete the configuration. The cmake tool will execute configuration and build compilation information, and wait for configure to complete its execution. After completion, we can modify some configuration information (such as closing some unnecessary compilation modules - BUILD_opencv_*), or keep the default. It is recommended to modify the python module support and installation manual

         This article cancels the python2 module (my computer has a 2 and 3 python versions installed) support, and modifies the installation directory:

        Click Genrate to generate the Makefile.

         2.2 Dynamic library compilation

        Enter the compilation output directory (opencv_build), start the command line tool

         For some MinGW versions, you need to go to mingw\include\c++\12.2.0\math.h:91 to comment out the execution statement using std::hypot;, because some versions do not contain hypot declaration definitions.

        Enter the command make to compile (the compilation time is longer, if you can use multi-core compilation to speed up the compilation time, make -j4, or mingw32-make, 4 is the number of cores):

        We can also compile on demand, the mingw32-make help command can output which modules can be compiled,

         mingw32-make module name can specify which modules to compile, such as mingw32-make opencv_python3

         When compiling mingw32-make opencv_python2, if python3 and python2 are installed on the computer, errors may occur, and you need to specify the location of the version yourself.

        If there is a problem in the compilation process, it is mainly to go to the cmake-gui configuration interface to check whether the configuration information is correct, mainly to check whether the dependency path of some compilation tools is correct, or to check the Makefile file information under the generated compilation path to make adjustments.

        After compiling, mingw32-make install installs:

        The final installation directory structure is as follows, the most important ones used are the include and lib directories:

        The installed dynamic library files are in the mingw/bin directory, as follows:

        Since my computer and the opencv library directory compiled by VC2015 are added to the environment path, this will not be added. Readers can add mingw/lib and mingw/bin to the environment variable settings by themselves.

         2.3 Static library compilation (optional)

        Static library compilation is to cancel the BUILD_SHARED_LIBS option in the cmake-gui interface:

        And turn on the BUILD_opencv_world option (dynamic libraries are not recommended, otherwise the program needs to be accompanied by a large dll library, which is not worth it), otherwise, when using static library links, you need to make a lot of static link configurations:

         Then generate a new Makefile again, and do a compilation process again similar to the dynamic library compilation. Since the dynamic library has been compiled before, this time it is mainly to generate a static library, so the compilation is relatively fast.

mingw32-make -j4
mingw32-make install

        After editing and installation, a staticlib directory will be created under the x64/mingw directory, which stores static libraries. After the option is enabled, there is only one library related to OpenCV, unlike dynamic libraries that generate a bunch of small file libraries by module. :

   3. Case test

        3.1 Engineering design and configuration

        Create a project as follows:

#一个读取图片的测试项目
test 
    bin
        2.bmp        #随便一张图片
    src
        main.cpp
    Makefile

        main.cpp

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: " << argv[0] << " ImageToLoadAndDisplay" << endl;
     return -1;
    }
    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file
    if( image.empty() ) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    }
    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image ); // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

        Makefile file configuration, mainly to include opencv header files and libraries used

#/bin/sh
CX= g++ 

BIN 		:= ./bin
TARGET      := testMinGW.exe
FLAGS		:= -std=c++11 
#-static
SRCDIR 		:= ./src
#INCLUDES
INCLUDEDIR 	:= -I"$(SRCDIR)" -I"../opencv_MinGW/include"
LIBDIR		:= -L"../opencv_MinGW/x64/mingw/bin" -lopencv_core460 -lopencv_highgui460 -lopencv_imgcodecs460
source		:= $(wildcard $(SRCDIR)/*.cpp) 

$(TARGET) :
	$(CX) $(FLAGS) $(INCLUDEDIR) $(source)  -o $(BIN)/$(TARGET) $(LIBDIR)

clean:
	rm  $(BIN)/$(TARGET)

        3.2 Dynamic use test

        Compile directly with make or mingw32-make command

         Since it is a dynamic library, you need to go to mingw/bin to copy the relevant dynamic library to the program output directory (if the dynamic library has been added to the environment variable, it is not necessary), and then run the output program:

        3.3 Static library usage test

        Now modify it to static compilation and modify the Makefile file:

#/bin/sh
CX= g++ 

BIN 		:= ./bin
TARGET      := testMinGW.exe
FLAGS		:= -std=c++11 -static
SRCDIR 		:= ./src
#INCLUDES
INCLUDEDIR 	:= -I"../opencv_MinGW/include" 
#-I"$(SRCDIR)"
staticDir   := ../opencv_MinGW/x64/mingw/staticlib/
LIBDIR		:= $(staticDir)/libopencv_world460.a\
			   $(staticDir)/libade.a \
			   $(staticDir)/libIlmImf.a \
			   $(staticDir)/libquirc.a \
			   $(staticDir)/libzlib.a \
			   $(wildcard $(staticDir)/liblib*.a) \
			   -lgdi32 -lComDlg32 -lOleAut32 -lOle32 -luuid 
#opencv_world放弃前,然后是opencv依赖的第三方库,后面的库是MinGW编译工具的库
#和后面这种库引用等价
#LIBDIR 	    := -L $(staticDir) -lopencv_world460 -lade -lIlmImf -lquirc -lzlib \
#				-llibjpeg-turbo -llibopenjp2 -llibpng -llibprotobuf -llibtiff -llibwebp \
#				-lgdi32 -lComDlg32 -lOleAut32 -lOle32 -luuid 

source		:= $(wildcard $(SRCDIR)/*.cpp) 

$(TARGET) :
	$(CX) $(FLAGS) $(INCLUDEDIR) $(source)  -o $(BIN)/$(TARGET) $(LIBDIR)

clean:
	rm  $(BIN)/$(TARGET)

        Compile make again (mingw32-make):

         Enter the bin directory, clear the dynamic library copied in when the dynamic library was compiled (shield the impact), and run the program, the same effect can be achieved:

         That's all for this article!

        Thank you readers for patiently reading to the end. My articles are very long and the knowledge points are very detailed. There may be omissions and mistakes. If there are any inadequacies, please point them out. If the content touches you, please like and follow to prevent getting lost

Guess you like

Origin blog.csdn.net/py8105/article/details/129845234