Compile OpenCV-4.5.3 source code through cmake-gui and VS2019 on Windows

Download OpenCV-4.5.3 source code

can be downloaded from github. There are many on the Internet. Find the tag tag, select OpenCV-4.5.3, enter it, and find sourceCode.zip. Then click Download.
As follows:
Download address
After entering, click the tags label in the picture below
Insert image description here
Insert image description here
Insert image description here
Insert image description here
You can also find a Linux cloud machine with a good network and directly enter wget https://github.com/opencv/opencv/archive/refs/tags/4.5.3.zip to download

Download opencv_contrib-4.5.3 source code

download link

It is also downloaded in the same way as above. Since opencv_contrib-4.5.3 is a module of opencv, this is needed when compiling OpenCV.

The prepared screenshots of the source code are as follows:
Insert image description here
Since github often cannot be opened, the source code above cannot be downloaded. Here is how to download the network disk:
Link: https://pan.baidu.com/s/1nTKa72azGfCXJynqwhEeFw
Extraction code: 14bz
Just unzip it after downloading.

Open cmake-gui

Insert image description here
has been installed before. If you have not installed it before, you can read this article:cmake installation
Insert image description here
Insert image description here

Select generator

Insert image description here
ps: The vcproj mentioned in the above picture is corrected to vcxproj.

After configuration, click Finish in the picture above.
Then it will automatically start to parse the configuration in the CmakeList.txt file, including automatically finding the required values ​​of various variables in the CmakeList.txt file from the current computer system environment. CmakeList.txt has been parsed. Whether each source file in exists. (Automatic recognition), the required libraries have been downloaded from third parties. After the configuration is parsed, the Name-Value display area in the middle will display the names of various variables in the parsed CmakeList.txt file, the values ​​that have been found from the current system environment variables, and the status of the current variables.
Today’s open source projects are basically managed using cmake. cmake is a project management tool. Organize the project's build steps according to the rules written in CmakeList.txt. Under a large open source project, there will be multiple modules, and each module will have its own CmakeList.txt file. But there is only one CmakeList.txt in the root directory of the open source project.
The CmakeList.txt file in the root directory will import the CmakeList.txt file under each module. This is somewhat similar to the role of maven in java.

Insert image description here

Here we paste the content printed at the bottom of the picture above to take a look.

Selecting Windows SDK version 10.0.22000.0 to target Windows #Select the appropriate Windows SDK on this machine
10.0.22621. Detected processor: AMD64 #Detected the local processor architecture< /span>

Found PythonInterp: D:/software/python/python.exe (found suitable version “3.8.6”, minimum required is “2.7”) #Detected that the python installed on this machine is 3.8.6. And the minimum requirement is 2.7 to meet the requirements . If you need to use OpenCV for python. Here you need to add python to the system environment variable Path to detect it.


下面的提示有个zip没有从网络上下载下来。
IPPICV: Download: ippicv_2020_win_intel64_20191018_general.zip
Try 1 failed

=======================================================================
  Couldn't download files from the Internet. #不能够从互联网上下载此文件
  Please check the Internet access on this host.
=======================================================================

CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
  IPPICV: Download failed: 6;"Couldn't resolve host name"

  For details please refer to the download log file:

  D:/vs_project/OpenCV/bulid_x64/CMakeDownloadLog.txt  #查看这个日志文件,手动下载此文件
OpenCV Python: during development append to PYTHONPATH: D:/vs_project/OpenCV/bulid_x64/python_loader
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 
FFMPEG: Download: opencv_videoio_ffmpeg.dll
Try 1 failed
.......................................
FFMPEG: Download: opencv_videoio_ffmpeg_64.dll
Try 1 failed

Contents in D:/vs_project/OpenCV/bulid_x64/CMakeDownloadLog.txt:
Insert image description here
According to the log file, three files failed to download:

  1. ippicv_2020_win_intel64_20191018_general.zip
  2. opencv_videoio_ffmpeg.dll
  3. opencv_videoio_ffmpeg_64.dll
    According to the log file, ippicv_2020_win_intel64_20191018_general.zip is obtained from https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af2 74/ippicv/ippicv_2020_win_intel64_20191018_general.zip path download File to D:/vs_project/OpenCV/opencv-4.5.3/.cache/ippicv/879741a7946b814455eee6c6ffde2984-ippicv_2020_win_intel64_20191018_general.zip.
    Insert image description here
    Download manually. I downloaded it from Huawei Cloud through wget.
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_win_intel64_20191018_general.zip

Insert image description here
Then manually add the uuid prefix and place it in the designated directory of Windows.
Insert image description here
The other two: https://raw.githubusercontent.com/opencv/opencv_3rdparty/213fcd5d4897319a83207406036c4a5957fba010/ffmpeg/opencv_videoio_ffmpeg.dll to D:/vs_project/OpenCV/opencv-4.5.3/.cache /ffmpeg/bab661341c30862fa88627130219c0a5-opencv_videoio_ffmpeg.dll

https://raw.githubusercontent.com/opencv/opencv_3rdparty/213fcd5d4897319a83207406036c4a5957fba010/ffmpeg/opencv_videoio_ffmpeg_64.dll to D:/vs_project/OpenCV/opencv-4.5.3/.cache/ffmpeg/ac99f9 767a83103c31709628af685924-opencv_videoio_ffmpeg_64.dll
is also downloaded according to the above method, and then placed in the specified directory.
Insert image description here
Downloading is slow…. After it is downloaded, just put it in the specified directory.
In fact, the above files are not downloaded and will not affect subsequent operations. It’s just that future exe, dll or lib files will not contain these functions.
Insert image description here
Insert image description here
Next, let’s talk about the settings in the red box:
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Next, enter the D:\vs_project\OpenCV\bulid_x64 directory, and you can see that through cmake Constructed project engineering. You can see .sln (solution files) and a large number of .vcxproj (project files) inside, as well as .vcxproj files under each module.
Insert image description here

Open the built .sln project file through Visual Studio 2019

In this way, the solution project on Visual Studio 2019 is built through cmake.
Double-click OpenCV.sln and it will automatically open with Visual Studio 2019.
Insert image description here

Perform compilation operations

Execute the ALL_BUILD command (the bottom layer will call the VS MSBuild command to read the content of the .vcxproj file and execute the entire process of compilation and linking through gl.exe) (release /x64)
Insert image description here
注意 : Although the newly built project through cmake is in the D:\vs_project\OpenCV\bulid_x64 directory, the source code of OpenCV is not in the bulid_x64 directory. Therefore, when ALL_BUILD is executed (when actually compiling the source code), the original source code location: D:\vs_project\OpenCV\opencv-4.5.3 and the CMakeLists.txt file under it still need to be referenced and used. Otherwise, an error will be reported in ALL_BUILD.
Insert image description here

Insert image description here

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
You can see that some projects generate lib libraries after final compilation, some generate dll libraries, and some generate exe files.
They are all placed in the D:\vs_project\OpenCV\bulid_x64\bin\Release directory.

Perform installation operations

Next execute INSTALL:
Insert image description here
After execution: an install directory will be created in the D:\vs_project\OpenCV\bulid_x64 directoryInsert image description here
Sometimes , there are multiple dll and lib files generated, sometimes only a few. It depends on whether BUILD_opencv_world is checked: if this item is not checked, many dll and lib files will be generated. If checked, only one or two library files may be generated. This includes opencv_world453.dll. This dll library includes other dll libraries.
Insert image description here
This completes the construction and compilation!
个人总结: When an open source project is released, CMake is used to manage the project structure. However, the .sln project built by using the specified generator through the cmake project is different from the .sln project actually created using VS. If we carry out secondary development, we still need to develop under CMake to manage the project. This constructed .sln project is only suitable for generating compiled results and publishing results.

Guess you like

Origin blog.csdn.net/adminstate/article/details/135019329
Recommended