源码编译思路:
- 下载QGIS源码+软件包(QGIS-final-3_10_4/cygwin/OSGeo4W/CMake )
- cygwin及OSGeo4W 安装依赖库(qgis源码编译的依赖库)
- 配置依赖库的环境变量
- CMake编译生成解决方案(qgis.sln)
- vs2017生成解决方案(qgis.sln),运行qgis.exe
A . QGIS源码+软件包下载路径
- qgis源码下载路径(QGIS-final-3_10_4):https://codeload.github.com/qgis/QGIS/zip/final-3_10_4
- CMake下载路径:https://cmake.org/files/v3.12/cmake-3.12.3-win64-x64.msi
- cygwin下载路径:http://cygwin.com/setup-x86.exe(32位)或http://cygwin.com/setup-x86_64.exe(64位)
- OSGeo4W 下载路径:http://download.osgeo.org/osgeo4w/osgeo4w-setup-x86.exe(32位)或http://download.osgeo.org/osgeo4w/osgeo4w-setup-x86_64.exe(64位)
- ninja下载路径:https://github.com/ninja-build/ninja/releases/download/v1.7.2/ninja-win.zip
B . 官网编译文档及版本约束
github源码编译指南:https://github.com/qgis/QGIS/blob/final-3_10_4/INSTALL
版本不匹配可能导致编译失败或者无法运行源代码,请按照github源码编译指南给定的版本信息进行版本选择!!!
所需的构建工具版本约束:
-CMake >= 3.0.0 |
Flex >= 2.5.6 |
Bison >= 2.4 |
Python 3 |
所需的构建依赖项版本约束:
Qt >= 5.9.0 |
- Proj >= 4.9.3 |
- GEOS >= 3.4 |
- Sqlite3 >= 3.0.0 |
- SpatiaLite >= 4.2.0 |
- libspatialindex |
- GDAL/OGR >= 2.1 |
- Qwt >= 5.0 & (< 6.1 with internal QwtPolar) |
- expat >= 1.95 |
- QScintilla2 |
- QCA |
- qtkeychain (>= 0.5) |
- libzip |
可选依赖项版本约束:
for GRASS providers and plugin - GRASS >= 7.0.0. |
- for georeferencer - GSL >= 1.8 |
- for PostGIS support - PostgreSQL >= 8.0.x |
- for gps plugin - gpsbabel |
- for mapserver export and PyQGIS - Python >= 3.3 |
- for python support - SIP >= 4.12, PyQt >= 5.3 must match Qt version, Qscintilla2 |
- for qgis mapserver - FastCGI |
- for oracle provider - Oracle OCI library |
C. 依赖库安装
OSGeo4W 安装步骤
打开osgeo4w-setup后,选择 Advanced Install -> Install from Internet ,之后开始选择要下载的依赖项。 这里下载位置一定要记好,与后续依赖库的环境变量配置有关!!!!!
下载镜像网站选择:
官网给定的选择“qgis-ltr-deps”软件包所依赖的软件包。如果安装其他软件包,则可能会导致问题。特别要确保不安装msinttypes软件包。它在以下位置安装一个stdint.h文件OSGeo4W [64] \ include,与Visual Studio自己的stdint.h冲突,后者用于该示例中断了虚拟层提供程序的构建。
cygwin安装步骤
下载安装路径,记录好,后面配置依赖库的环境变量要用!!!!
需要下载安装 bison、 flex、 git(下载版本约束B有介绍)
D. 配置环境变量(F:\QGIS-final-3_10_4\ms-windows\osgeo4w\msvc-env.bat修改为如下,注释用“\\注释内容”也写在下面了,如果下载安装路径和我一致,直接删除注释内容即可)
@echo off
REM ***************************************************************************
REM msvc-env.cmd
REM ---------------------
REM begin : June 2018
REM copyright : (C) 2018 by Juergen E. Fischer
REM email : jef at norbit dot de
REM ***************************************************************************
REM * *
REM * This program is free software; you can redistribute it and/or modify *
REM * it under the terms of the GNU General Public License as published by *
REM * the Free Software Foundation; either version 2 of the License, or *
REM * (at your option) any later version. *
REM * *
REM ***************************************************************************
set ARCH=%1
if not "%ARCH%"=="x86" if not "%ARCH%"=="x86_64" (
goto usage
)
//配置OSGeo4W路径
if "%OSGEO4W_ROOT%"=="" (
if "%ARCH%"=="x86" (
set OSGEO4W_ROOT=C:\OSGeo4W
set VCARCH=x86
) else (
set OSGEO4W_ROOT=C:\OSGeo4W64
set VCARCH=amd64
)
)
if not exist "%OSGEO4W_ROOT%\bin\o4w_env.bat" (echo o4w_env.bat not found & goto error)
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
if not "%PROGRAMFILES(X86)%"=="" set PF86=%PROGRAMFILES(X86)%
if "%PF86%"=="" set PF86=%PROGRAMFILES%
if "%PF86%"=="" (echo PROGRAMFILES not set & goto error)
//配置vs2017路径(VS150COMNTOOLS是2017要注意一下)
set VS150COMNTOOLS = C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvarsall.bat x64
//配置vs2015路径(VS140COMNTOOLS是2015要注意一下)
set VS140COMNTOOLS=D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
call "D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" %VCARCH%
path %path%;D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
//配置grass7的路径
set GRASS7=
if exist %OSGEO4W_ROOT%\bin\grass78.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass78.bat
if exist %OSGEO4W_ROOT%\bin\grass74.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass74.bat
if exist %OSGEO4W_ROOT%\bin\grass76.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass76.bat
if "%GRASS7%"=="" (echo GRASS7 not found & goto error)
for /f "usebackq tokens=1" %%a in (`%GRASS7% --config path`) do set GRASS_PREFIX=%%a
//配置dll文件路径
set PYTHONPATH=
if exist C:\CMake\bin path %PATH%;C:\CMake\bin
if exist c:\cygwin64\bin path %PATH%;c:\cygwin64\bin
if exist c:\cygwin\bin path %PATH%;c:\cygwin\bin
path
//配置lib文件路径
set LIB=%LIB%;%OSGEO4W_ROOT%\apps\Qt5\lib;%OSGEO4W_ROOT%\lib
//配置include头文件路径
set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\apps\Qt5\include;%OSGEO4W_ROOT%\include
goto end
:usage
echo usage: %0 arch
echo sample: %0 x86_64
pause
exit /b 1
:error
echo ENV ERROR %ERRORLEVEL%: %DATE% %TIME%
pause
exit /b 1
:end
pause
C:\OSGeo4W64下新建test.bat内容如下
@echo off
call F:\QGIS-final-3_10_4\ms-windows\osgeo4w\msvc-env.bat x86_64
@cmd
cmd运行该test.bat
说明临时环境变量配置成功!!继续运行cmake-gui
下面就是用cmake编译生成qgis.sln编译文件
vs版本和编译平台配置如下
配置后就点击configure,当configure done后点击generate 如果出现generate done就说明生成成功,配置信息如下(依赖库的所有路径都有设置(按照之前配置的环境变量自动设置))
如果出错了,就看报错的是什么一般都是依赖库缺失,到cygwin和OSGeo4W中查找对应的库进行下载,没错直接open project。如果想快点编译好并运行,可以只保留这几个进行编译。
关于编译出现的问题,可以百度,这里只介绍最常见的问题,例如“(”错误什么的,一般都是编码问题,不会让你去改源码的,在对应项目的C/C++命令行输入/source-charset:utf-8 即可。
运行时可能出现,dll未能加载、缺失或者错误的问题,例如
用depends.exe查一下,该dll的依赖项,可能他依赖的dll你缺失了,或者没有配置到环境变量中导致的加载失败,depends加载dll的过程可能有些漫长,要耐心等待一下。
配置环境变量路径或者把这个缺失的dll放在F:\QGIS-final-3_10_4\build\output\bin\RelWithDebInfo 生成目录下,即可解决该问题。然后就可以使用啦。