源碼編譯思路:
- 下載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 生成目錄下,即可解決該問題。然後就可以使用啦。