1. qgis二次开发环境搭建(qgis-3.28.6+qt5.
推荐
推荐开源代码QGis C++ API二次开发示例大全


前言
本文介绍了QGIS二次开发环境的搭建,完成了以下工作
- ubuntu22.04下,QGIS3.28.8编译
- ubuntu22.04下,使用QCreator调试QGIS3.28.8
- ubuntu22.04下,新建工程,引入编译好的QGIS库,并成功调用Qgis::releaseName()
- Windows10下,安装QGIS以及Release开发库
- Windows10下,新建工程,引入QGIS库,并成功调用Qgis::releaseName()
QGIS3.28.8下载地址
ubuntu22.04
- ubuntu系统编译参考官网
-
这里要注意ubuntu的版本,之前在ubuntu20.04上尝试编译QGIS3.28.8失败,由于ubuntu20.04默认安装的是Qt5.12,而QGIS3.28是要求Qt>=5.14的,下图是依赖的库,下一章有apt安装命令
image.png
QGIS3.28.8编译
以下按照官网编译文档进行操作:
-
3.2. Prepare apt
image.png
- 3.3. Install build dependencies
sudo apt-get install bison build-essential ca-certificates ccache cmake cmake-curses-gui dh-python doxygen expect flex flip gdal-bin git graphviz grass-dev libdraco-dev libexiv2-dev libexpat1-dev libfcgi-dev libgdal-dev libgeos-dev libgsl-dev libpdal-dev libpq-dev libproj-dev libprotobuf-dev libqca-qt5-2-dev libqca-qt5-2-plugins libqscintilla2-qt5-dev libqt5opengl5-dev libqt5serialport5-dev libqt5sql5-sqlite libqt5svg5-dev libqt5webkit5-dev libqt5xmlpatterns5-dev libqwt-qt5-dev libspatialindex-dev libspatialite-dev libsqlite3-dev libsqlite3-mod-spatialite libyaml-tiny-perl libzip-dev libzstd-dev lighttpd locales ninja-build ocl-icd-opencl-dev opencl-headers pandoc pdal pkg-config poppler-utils protobuf-compiler pyqt5-dev pyqt5-dev-tools pyqt5.qsci-dev python3-all-dev python3-autopep8 python3-dev python3-gdal python3-jinja2 python3-lxml python3-mock python3-nose2 python3-owslib python3-plotly python3-psycopg2 python3-pygments python3-pyproj python3-pyqt5 python3-pyqt5.qsci python3-pyqt5.qtmultimedia python3-pyqt5.qtpositioning python3-pyqt5.qtsql python3-pyqt5.qtsvg python3-pyqt5.qtwebkit python3-pyqtbuild python3-sip python3-termcolor python3-yaml qt3d-assimpsceneimport-plugin qt3d-defaultgeometryloader-plugin qt3d-gltfsceneio-plugin qt3d-scene2d-plugin qt3d5-dev qtbase5-dev qtbase5-private-dev qtkeychain-qt5-dev qtmultimedia5-dev qtpositioning5-dev qttools5-dev qttools5-dev-tools sip-tools spawn-fcgi xauth xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable xvfb
选择ubuntu22.04对应的代号jammy


- 3.4. Setup ccache (Optional, but recommended)
cd /usr/local/bin
sudo ln -s /usr/bin/ccache gcc
sudo ln -s /usr/bin/ccache g++
- 3.5. Prepare your development environment
mkdir -p ${HOME}/dev/cpp
cd ${HOME}/dev/cpp
-
3.6. Check out the QGIS Source Code
这里直接用上边下载好的源码
image.png
- 3.7. Starting the compile
mkdir -p ${HOME}/dev/cpp/apps //创建安装目录
cd /home/t/dev/cpp/qgis-3.28.8
mkdir build
cd build
ccmake ..


这里可以配置编译选项

首先输入c进行configure,进行选项修改,分别修改
- CMAKE_BUILD_TYPE为Debug,
-
CMAKE_INSTALL_PREFIX为/home/t/dev/cpp/apps。
image.png

-
enter确认,再次输入c进行configure,输入e返回,输入g进行生成
image.png
编译
make -jX //X是数字,指使用几个核心进行编译 编译过程较长,持续数分钟或更长


编译结果位于output文件夹

也可以安装到前边设置的目录
make install
运行
/home/t/dev/cpp/qgis-3.28.8/build/output/bin/qgis


QCreator调试QGIS
-
打开QGIS工程
image.png
-
设置项目
image.png

-
编译 这一步很慢,可能因为没有设置make -jX吧
image.png
-
调试运行
image.png

新建工程,链接QGIS库,调用Qgis::releaseName()
-
新建工程
image.png
-
CMakeLists.txt添加头文件和库目录
include_directories(/home/t/dev/cpp/apps/qgis/include/qgis) # 指定qgis头文件路径
link_directories(/home/t/dev/cpp/apps/qgis/lib/) # 指定qgis库搜索路径
target_link_libraries(qgis_demo
Qt${QT_VERSION_MAJOR}::Widgets
qgis_app.so
qgis_core.so
qgis_gui.so
qgis_analysis.so)
-
调用Qgis::releaseName()函数
image.png
-
QGIS库编译的是Debug版本,所以可以跟踪调试QGis内部函数
image.png
Windows10
- Windows下QGIS Debug比较费劲,所以这里只搭建了Release环境
- Qt5.15.2
安装QGIS以及Release开发库
OSGeo成立于2006年,主要为自由和开源的地理空间社区提供经济、组织和法律上的帮助。如今,OSGeo孵化并支持了大量的开源GIS软件和工具。
-
OSGeo4W下载安装
image.png





-
选择qgis及其开发库
选择qgis-ltr和qgis-ltr-dev,版本3.28
image.png
image.png
image.png
-
下载安装完成后位置为
image.png
新建工程,链接QGIS库,调用Qgis::releaseName()
-
新建工程
image.png

-
设置QGis头文件和库地址
image.png
INCLUDEPATH += C:\\OSGeo4W\\apps\\qgis-ltr-dev\\include
LIBS += -L"C:\\OSGeo4W\\apps\\qgis-ltr-dev\\lib" -lqgis_app -lqgis_core -lqgis_gui -lqgis_native
- 拷贝dll 从目录
C:\OSGeo4W\bin
拷贝至程序运行目录build-qgis_demo_w-Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release
,以及其他报错的dll如Qt5WebKitWidgets.dll如下图
image.png
-
调用Qgis::releaseName()函数
image.png
遇到的问题
- error C2065: “M_PI”: 未声明的标识符
add_definitions(-D_USE_MATH_DEFINES)
2.warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
add_compile_options(/wd4819)
-
fatal error C1083: 无法打开包括文件: “ogr_api.h”: No such file or directory
image.png
安装gdal即可