The screenshot below is my Cmake configuration for QGIS source code compilation. I found many versions of QGIS source code configuration. The core is actually the environment variable setting.
Create a new bat file, open the bat file and start Cmake
@echo off set VS160COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 set INCLUDE=%INCLUDE%;C:\Program Files (x86)\Windows Kits\10\Include set LIB=%LIB%;C:\Program Files (x86)\Windows Kits\10\Lib set OSGEO4W_ROOT=D:\OSGeo4W call "%OSGEO4W_ROOT%\bin\o4w_env.bat" call "%OSGEO4W_ROOT%\bin\py3_env.bat" call "%OSGEO4W_ROOT%\bin\qt5_env.bat" set PYTHONPATH= if exist "C:\Program Files\CMake\bin" path %PATH%;C:\Program Files\CMake\bin if exist D:\cygwin64\bin path %PATH%;D:\cygwin64\bin SET PYTHONHOME=D:\OSGeo4W\apps\Python39 SET PYTHONPATH=D:\OSGeo4W\apps\Python39\Scripts set PYTHON_EXECUTABLE=D:/OSGeo4W/apps/Python39/python3.exe set PYTHON_LIBRARY=D:/OSGeo4W/apps/Python39/libs/python39.lib set PYTHON_INCLUDE_PATH=D:\OSGeo4W\apps\Python39\include PATH%PYTHONPATH%;%PATH% path %OSGEO4W_ROOT%\apps\Qt5\bin;%PATH% set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins set O4W_QT_PREFIX=%OSGEO4W_ROOT:\=/%/apps/Qt5 set O4W_QT_BINARIES=%OSGEO4W_ROOT:\=/%/apps/Qt5/bin set O4W_QT_PLUGINS=%OSGEO4W_ROOT:\=/%/apps/Qt5/plugins set O4W_QT_LIBRARIES=%OSGEO4W_ROOT:\=/%/apps/Qt5/lib set O4W_QT_TRANSLATIONS=%OSGEO4W_ROOT:\=/%/apps/Qt5/translations set O4W_QT_HEADERS=%OSGEO4W_ROOT:\=/%/apps/Qt5/include set O4W_QT_DOC=%OSGEO4W_ROOT:\=/%/apps/Qt5/doc set O4W_ROOT=%OSGEO4W_ROOT:\=/% set LIB_DIR=%O4W_ROOT% path %path%;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64 path %PATH%;D:\OSGeo4W\apps\Qt5\bin;D:\OSGeo4W\apps\bin;D:\cygwin64\bin path %OSGEO4W_ROOT%\apps\gdal-dev\bin;%PATH% @set GRASS_PREFIX=D:\OSGeo4W\apps\grass\grass78 @set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\include @set LIB=%LIB%;%OSGEO4W_ROOT%\lib;%OSGEO4W_ROOT%\lib set LIB=%LIB%;%OSGEO4W_ROOT%\apps\Qt5\lib;%OSGEO4W_ROOT%\lib set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\apps\Qt5\include;%OSGEO4W_ROOT%\include call "C:\Program Files\CMake\bin\cmake-gui.exe"
Things to modify after CMake-gui
(1) Add /MP /utf-8 after CMAKE_CXX_FLAGS to speed up compilation and solve the problem of unrecognizable characters.
(2) Uncheck ENABLE_TESTS, WITH_BINDINGS, WITH_QTWEBKIT, WITH_GRASS7, if TESTS project is required,
You don’t need to remove it, but the others must be removed, because there is no installation-related dependencies. If necessary, please do your own research or follow my follow-up tutorials.
(3) Check WITH_CUSTOM_WIDGETS, WITH_QSPATIALITE;
(4) Modify the path of PYTHON_EXECUTABLE to C:\OSGeo4W64\apps\Python37\python.exe, which must be modified here,
The path recognized by the system will report python’s setlocal error when compiling.
(5) Set the INCLUDE_DIR and LIBRARY paths of Qwt, QCA, and Qtkeychain in the configuration item,
It should be noted that the INCLUDE_DIR of QCA must point to the QtCrypto directory, and the INCLUDE_DIR of Qtkeychain must point to the qt5keychain directory.
(6) If you need to define the installation path of QGIS, please modify CMAKE_INSTALL_PREFIX.
Start the bat file of VS to open VS and compile the source code (the environment variable cannot be temporarily set without bat startup, and cannot be compiled)
@echo off set VS160COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 set INCLUDE=%INCLUDE%;C:\Program Files (x86)\Windows Kits\10\Include set LIB=%LIB%;C:\Program Files (x86)\Windows Kits\10\Lib set OSGEO4W_ROOT=D:\OSGeo4W call "%OSGEO4W_ROOT%\bin\o4w_env.bat" set PYTHONPATH= if exist "C:\Program Files\CMake\bin" path %PATH%;C:\Program Files\CMake\bin if exist D:\cygwin64\bin path %PATH%;D:\cygwin64\bin SET PYTHONHOME=D:\OSGeo4W\apps\Python39 SET PYTHONPATH=D:\OSGeo4W\apps\Python39\Scripts set PYTHON_EXECUTABLE=D:/OSGeo4W/apps/Python39/python3.exe set PYTHON_LIBRARY=D:/OSGeo4W/apps/Python39/libs/python39.lib set PYTHON_INCLUDE_PATH=D:\OSGeo4W\apps\Python39\include PATH%PYTHONPATH%;%PATH% path %OSGEO4W_ROOT%\apps\Qt5\bin;%PATH% set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins set O4W_QT_PREFIX=%OSGEO4W_ROOT:\=/%/apps/Qt5 set O4W_QT_BINARIES=%OSGEO4W_ROOT:\=/%/apps/Qt5/bin set O4W_QT_PLUGINS=%OSGEO4W_ROOT:\=/%/apps/Qt5/plugins set O4W_QT_LIBRARIES=%OSGEO4W_ROOT:\=/%/apps/Qt5/lib set O4W_QT_TRANSLATIONS=%OSGEO4W_ROOT:\=/%/apps/Qt5/translations set O4W_QT_HEADERS=%OSGEO4W_ROOT:\=/%/apps/Qt5/include set O4W_QT_DOC=%OSGEO4W_ROOT:\=/%/apps/Qt5/doc set O4W_ROOT=%OSGEO4W_ROOT:\=/% set LIB_DIR=%O4W_ROOT% path %path%;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64 path %PATH%;D:\OSGeo4W\apps\Qt5\bin;D:\OSGeo4W\apps\bin;D:\cygwin64\bin path %OSGEO4W_ROOT%\apps\gdal-dev\bin;%PATH% @set GRASS_PREFIX=D:\OSGeo4W\apps\grass\grass78 @set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\include @set LIB=%LIB%;%OSGEO4W_ROOT%\lib;%OSGEO4W_ROOT%\lib set LIB=%LIB%;%OSGEO4W_ROOT%\apps\Qt5\lib;%OSGEO4W_ROOT%\lib set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\apps\Qt5\include;%OSGEO4W_ROOT%\include call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe"
Many problems are QGIS version compatibility issues.