下面的截图是我进行QGIS源码编译的Cmake配置,找了好多个版本的QGIS源码配置,核心其实是环境变量设置。
新建bat文件,打开bat文件启动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"
CMake-gui后要修改的东西
(1)在CMAKE_CXX_FLAGS后加入 /MP /utf-8,用来加快编译速度和解决无法识别字符的问题。
(2)去除ENABLE_TESTS、WITH_BINDINGS、WITH_QTWEBKIT、WITH_GRASS7的勾选,如果需要TESTS工程,
可以不去除,其他的必须去除,因为没有安装相关依赖,如果需要请自行研究或者关注我后续的教程。
(3)勾选WITH_CUSTOM_WIDGETS、WITH_QSPATIALITE;
(4)修改PYTHON_EXECUTABLE的路径为C:\OSGeo4W64\apps\Python37\python.exe,此处必须进行修改,
系统识别到的路径在编译的时候会报python的setlocal错误。
(5)设置配置项中Qwt、QCA、Qtkeychain的INCLUDE_DIR和LIBRARY路径,
需要注意的是QCA的INCLUDE_DIR一定要指到QtCrypto目录,Qtkeychain的INCLUDE_DIR一定要指到qt5keychain目录。
(6)如果需要定义QGIS的安装路径,请修改CMAKE_INSTALL_PREFIX。
启动VS的bat文件打开VS并编译源码(不用bat启动无法临时设置环境变量,无法编译)
@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"
很多问题都是QGIS版本兼容性问题。