QGIS3.4.3+VS2015+Win10环境编译

QGIS3.4.3版本编译

一、前置环境

0.Win10操作系统

1.VS2015已安装

2.qgis 3.4.3源代码版本已下载并解压

解压某个目录,列如我的目录为:H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3

3.环境下载

​ 参考QGIS3.16.3+VS2015+Win10环境编译中的环境下载。本篇使用同样的Cygwin64和OSGeo4W包

二、源代码修改

1.解决后边编译可能出现“ C2001:常量中有换行符”或英文报错“Newline in constant”问题

​ 原因:QGIS43.4.3 源代码为 utf-8 编码,本机 locale 为中文,

​ 解决办法:需显示告诉编译器源码为 utf-8 :

​ [1]如果在Visual Stduio中如何修改?

​ 右键-Properties-- c/c++,Command Line,Addtional Options 里添加:/source-charset:utf-8

​ [2]camke中如何修改?(因为源代码是使用的cmake方式,主要讲解cmake中如何修改)

​ MSVC编译器设置源文件为UTF-8编码 ,需要在CMakeLists.txt文件中的添加如下代码

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/source-charset:utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")

[3] 主要在这些目录下的CMakeLists.txt文件

  • gui,app,core,analysis目中的CMakeLists.txt文件

  • src/provider目录下的

    postgresprovider_a
    arcgisrest(对应了项目 arcgisfeatureserverprovider_a)
    wmsprovider_a
    spatialiteprovider
    wcsprovider
    wfsprovider
    arcgismapserverprovider
    wmsprovider
    arcgisfeatureserverprovider
    geonodeprovider
    db2provider
    gdalprovider
    owsprovider
    oracleprovider
    mssqlprovider
    ogrprovider

  • src/plugins目录下的

    geometrycheckerplugin
    georefplugin
    offlineeditingplugin
    gpsimporterplugin

  • /tests/src/core目录(对应了项目qgis_expressiontest)

  • /src/auth/identcert (对应项目identcertauthmethod)

2.为解决如下编译报错问题。

CMake Error at src/providers/mdal/CMakeLists.txt:133 (ADD_LIBRARY):
Target "mdalprovider" links to target "Threads::Threads" but the target was
not found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?

CMake Error at src/providers/mdal/CMakeLists.txt:133 (ADD_LIBRARY):
Target "mdalprovider" links to target "Threads::Threads" but the target was
not found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?

需修改源代码src/providers/mdal/CMakeLists.txt文件。主要参考了其他高版本QGIS的这个文件如何编写.

在如下代码的前一句

FIND_PACKAGE(HDF5)

加上

if(MSVC)
  # HDF5 with enabled thread safety (HDF5_ENABLE_THREADSAFE=ON) on Windows needs Threads::Threads
  find_package(Threads)
endif()

3.修改core中的qgscoordinatereferencesystem.cpp文件

注释如下代码

  // try fixed up version
    OSRFixup( d->mCRS );

三、环境配置

1.新建目录dependences

​ 目录路径为H:\DevOps\QGIS3_4_3\dependences

2.剪切已下载的cywin64和OSGeo4W

​ 剪切到 H:\DevOps\QGIS3_4_3\dependences目录下)

3.修改源代码目录下的msvc-env.bat文件

用文本编辑器打开 H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3\ms-windows\osgeo4w\msvc-env.bat文件

[1]修改VS2015配置

​ 因为笔者VS2015安装路径是默认安装的,所有vs2015这一块不需要修改。

[2]修改OSGEO4W_ROOT路径

set OSGEO4W_ROOT=C:\OSGeo4W

修改为自己的OSGeo4W的路径

set OSGEO4W_ROOT=H:\DevOps\QGIS-3_4_3\dependences\OSGeo4W

[3]修改OSGEO4W_ROOT路径

[4]修改PYTHONPATH

set PYTHONPATH=
if exist "%PROGRAMFILES%\CMake\bin" path %PATH%;%PROGRAMFILES%\CMake\bin
if exist "%PF86%\CMake\bin" path %PATH%;%PF86%\CMake\bin
if exist c:\cygwin64\bin path %PATH%;c:\cygwin64\bin
if exist c:\cygwin\bin path %PATH%;c:\cygwin\bin

这里要说明下:这里实际上将camek的bin及cygwin64中的bin添加到环境变量的path中,而cygwin64中有flex.exe bison.exe cmake.exe等内容,这就是前面讲到你可以不装Cmake,如果没有装会在cygwin64中找。

因为c:\cygwin64目录已被我们截切到了H:\DevOps\QGIS3_4_3\dependences目录下,所以为了找到cygwin64目录。在上面的代码后面添加如下代码

if exist H:\DevOps\QGIS3_4_3\dependences\cygwin64\bin path %PATH%;H:\DevOps\QGIS3_4_3\dependences\cygwin64\bin

[5]配置grass

进行如下配置,否则编译过程会报grass not found

在下面代码后面

set GRASS7=
if exist %OSGEO4W_ROOT%\bin\grass72.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass72.bat
if exist %OSGEO4W_ROOT%\bin\grass74.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass74.bat

添加如下代码,因为H:\DevOps\QGIS-3_4_3\dependences\OSGeo4W\bin目录下只有grass78.bat.

if exist %OSGEO4W_ROOT%\bin\grass78.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass78.bat

最后完整代码如下

set ARCH=%1
if not "%ARCH%"=="x86" if not "%ARCH%"=="x86_64" (
	goto usage
)
if "%OSGEO4W_ROOT%"=="" (
	if "%ARCH%"=="x86" (
		set OSGEO4W_ROOT=H:\DevOps\QGIS3_4_3\dependences\OSGeo4W
		set VCARCH=x86
	) else (
		set OSGEO4W_ROOT=H:\DevOps\QGIS3_4_3\dependences\OSGeo4W
		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)

set VS140COMNTOOLS=%PF86%\Microsoft Visual Studio 14.0\Common7\Tools\
call "%PF86%\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" %VCARCH%
path %path%;%PF86%\Microsoft Visual Studio 14.0\VC\bin

set GRASS7=
if exist %OSGEO4W_ROOT%\bin\grass72.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass72.bat
if exist %OSGEO4W_ROOT%\bin\grass74.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass74.bat
if exist %OSGEO4W_ROOT%\bin\grass78.bat set GRASS7=%OSGEO4W_ROOT%\bin\grass78.bat
if "%GRASS7%"=="" (echo GRASS7 not found & goto error)
for /f "usebackq tokens=1" %%a in (`%GRASS7% --config path`) do set GRASS_PREFIX=%%a

set PYTHONPATH=
if exist "%PROGRAMFILES%\CMake\bin" path %PATH%;%PROGRAMFILES%\CMake\bin
if exist "%PF86%\CMake\bin" path %PATH%;%PF86%\CMake\bin
if exist c:\cygwin64\bin path %PATH%;c:\cygwin64\bin
if exist c:\cygwin\bin path %PATH%;c:\cygwin\bin
if exist H:\DevOps\QGIS3_4_3\dependences\cygwin64\bin path %PATH%;H:\DevOps\QGIS3_4_3\dependences\cygwin64\bin
path

set LIB=%LIB%;%OSGEO4W_ROOT%\apps\Qt5\lib;%OSGEO4W_ROOT%\lib
set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\apps\Qt5\include;%OSGEO4W_ROOT%\include

goto end

:usage
echo usage: %0 arch
echo sample: %0 x86_64
exit /b 1

:error
echo ENV ERROR %ERRORLEVEL%: %DATE% %TIME%
exit /b 1
:end

3.修改源代码目录下的package-nightly.cmd文件配置SETUPAPI_LIBRARY

​ 如果不配置或配置有问题,构建过程会报SETUPAPI_LIBRARY not found问题

:cmake_x86_64
set CMAKE_COMPILER_PATH=%PF86%\Microsoft Visual Studio 14.0\VC\bin\amd64
set SETUPAPI_LIBRARY=%PF86%\Windows Kits\10\Lib\10.0.14393.0\um\x64\SetupAPI.Lib
if not exist "%SETUPAPI_LIBRARY%" set SETUPAPI_LIBRARY=%PF86%\Windows Kits\8.0\Lib\win8\um\x64\SetupAPI.Lib

根据上面的信息可以知道可以配置10.0.14393.0或者win8中的SetupAPI.Lib,根据实际情况修改SETUPAPI_LIBRARY的值。列如我的机器上路径是C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64\SetupAPI.Lib。我这为了方便在上面的语句后面添加一句话,

if not exist "%SETUPAPI_LIBRARY%" set SETUPAPI_LIBRARY=%PF86%\Windows Kits\8.1\Lib\winv6.3\um\x64\SetupAPI.Lib

4.打开源代码目录下package.cmd文件检查Qgis依赖路径是否正确

    -D CMAKE_PDB_OUTPUT_DIRECTORY_RELEASE=%BUILDDIR%\apps\%PACKAGENAME%\pdb ^
	-D CMAKE_BUILD_TYPE=%BUILDCONF% ^
	-D CMAKE_CONFIGURATION_TYPES=%BUILDCONF% ^
	-D GEOS_LIBRARY=%O4W_ROOT%/lib/geos_c.lib ^
	-D SQLITE3_LIBRARY=%O4W_ROOT%/lib/sqlite3_i.lib ^
	-D SPATIALITE_LIBRARY=%O4W_ROOT%/lib/spatialite_i.lib ^
	-D PYTHON_EXECUTABLE=%O4W_ROOT%/bin/python3.exe ^
	-D SIP_BINARY_PATH=%PYTHONHOME:\=/%/sip.exe ^
	-D PYTHON_INCLUDE_PATH=%PYTHONHOME:\=/%/include ^
	-D PYTHON_LIBRARY=%PYTHONHOME:\=/%/libs/%PYVER%.lib ^
	-D QT_LIBRARY_DIR=%O4W_ROOT%/lib ^
	-D QT_HEADERS_DIR=%O4W_ROOT%/apps/qt5/include ^
	-D CMAKE_INSTALL_PREFIX=%O4W_ROOT%/apps/%PACKAGENAME% ^
	-D FCGI_INCLUDE_DIR=%O4W_ROOT%/include ^
	-D FCGI_LIBRARY=%O4W_ROOT%/lib/libfcgi.lib ^
	-D QCA_INCLUDE_DIR=%OSGEO4W_ROOT%\apps\Qt5\include\QtCrypto ^
	-D QCA_LIBRARY=%OSGEO4W_ROOT%\apps\Qt5\lib\qca-qt5.lib ^
	-D QSCINTILLA_LIBRARY=%OSGEO4W_ROOT%\apps\Qt5\lib\qscintilla2.lib ^

说明:这个文件基本上不需要做任何改动

5.在OSGeo4W路径下创建批处理文件OSGeo4W-dev.bat和OSGeo4W-vs.bat

[1]创建OSGeo4W-dev.bat(初始化环境),添加内容如下:

@echo off
call H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3\ms-windows\osgeo4w\msvc-env.bat x86_64
cd /d  H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3\ms-windows\osgeo4w
@cmd

[1]创建OSGeo4W-vs.bat (初始化环境,并在环境下运行vs2017),添加内容如下:

@echo off
call H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3\ms-windows\osgeo4w\msvc-env.bat x86_64
cd /d  H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3\ms-windows\osgeo4w
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe" qgis.sln
@cmd

四、构建

  1. 运行OSGeo4W-dev.bat初始化环境。

  2. 在命令行中运行configonly.bat。

    等待构建,若结果为completely,且在源码目录 H:\DevOps\QGIS3_4_3\QGIS-final-3_4_3\ms-windows\osgeo4w下生成了build-qgis-test-x86_64文件夹,则说明配置成功。构建过程中输出信息如下:

    -- Selecting Windows SDK version  to target Windows 10.0.19044.
    -- The C compiler identification is MSVC 19.0.24215.1
    -- The CXX compiler identification is MSVC 19.0.24215.1
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe - works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe - works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- QGIS version: 3.4.3 Madeira (30403)
    -- Looking for CL_VERSION_2_2
    -- Looking for CL_VERSION_2_2 - found
    -- Found OpenCL: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/opencl.lib (found version "2.2")
    -- Found OpenCL C++ headers: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/include
    -- Could not find GRASS 7
    -- Looking for proj_info in H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/proj_i.lib
    -- Looking for proj_info in H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/proj_i.lib - found
    -- Found Proj: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/proj_i.lib (6.3.2)
    -- Found GEOS: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/geos_c.lib (3.8.1)
    -- Found GDAL: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/gdal_i.lib (3.1.4)
    -- Found Expat: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/expat.lib
    -- Found Spatialindex: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/spatialindex-64.lib
    -- Found Qwt: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/apps/Qt5/lib/qwt.lib (6.1.3)
    -- Found libzip: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/zip.lib
    -- Found Sqlite3: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/sqlite3_i.lib
    -- Found PostgreSQL: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/libpq.lib
    -- Found SpatiaLite: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/spatialite_i.lib
    -- Looking for gaiaStatisticsInvalidate in H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/spatialite_i.lib
    -- Looking for gaiaStatisticsInvalidate in H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/spatialite_i.lib - found
    -- Qt WebKit support enabled
    -- Found Qt version: 5.11.2
    -- Found QScintilla2: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/apps/Qt5/lib/qscintilla2.lib (2.10.8)
    -- Found QtKeychain: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/apps/Qt5/lib/qt5keychain.lib
    -- Found QCA: H:\DevOps\QGIS3_~1\DEPEND~1\OSGeo4W\apps\Qt5\lib\qca-qt5.lib (2.1.3)
    -- Pedantic compiler settings enabled
    -- Debug output enabled
    -- Found PythonInterp: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/bin/python3.exe (found suitable version "3.7", minimum required is "3")
    -- Found Python executable: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/bin/python3.exe
    -- Found Python version: 3.7.0
    -- Found Python library: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/apps/Python37/libs/python37.lib
    -- Found Python site-packages: H:/DevOps/QGIS3_~1/DEPEND~1/OSGeo4W/apps/Python37/Lib/site-packages
    -- Found PyQt5 version: 5.11.3
    -- Found SIP version: 4.19.14
    -- Found QScintilla2 PyQt module: 2.10.8
    -- txt2tags not found - disabled
    -- Performing Test COMPILER_HAS_DEPRECATED_ATTR
    -- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Failed
    -- Performing Test COMPILER_HAS_DEPRECATED
    -- Performing Test COMPILER_HAS_DEPRECATED - Success
    -- Found HDF5: hdf5::hdf5-shared (found version "1.10.5")
    -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
    -- Found NetCDF: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/netcdf.lib
    -- Found OCI: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/oci.lib
    -- Found GSL: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/gsl.lib;H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/cblas.lib
    -- Found FCGI: H:/DevOps/QGIS3_4_3/dependences/OSGeo4W/lib/libfcgi.lib
    CMake Warning (dev) in i18n/CMakeLists.txt:
      A logical block opening on the line
    
        H:/DevOps/QGIS3_4_3/QGIS-final-3_4_3/i18n/CMakeLists.txt:28 (IF)
    
      closes on the line
    
        H:/DevOps/QGIS3_4_3/QGIS-final-3_4_3/i18n/CMakeLists.txt:42 (ENDIF)
    
      with mis-matching arguments.
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    -- Qsci sip file not found - disabling bindings for derived classes
    -- Ctest Binary Directory set to: H:/DevOps/QGIS3_4_3/QGIS-final-3_4_3/ms-windows/osgeo4w/build-qgis-test-x86_64/output/bin
    -- Configuring done
    

五、编译

  1. 运行OSGeo4W-vs.bat,初始化环境并在环境下运行vs2015并打开qgis.sln解决方案
  2. 在RelWithDebInfo模式下进行编译ALL_BUILD。
  3. 编译完成后调试qgis项目,运行成功,则编译完成。

六、编译报错问题

参考QGIS3.16.3+VS2015+Win10环境编译 中编译报错的问题解决办法

posted @ 2022-04-22 23:23  焦涛  阅读(675)  评论(0)    收藏  举报