QGIS secondary development 2: Do not recompile QGIS for secondary development

Table of contents

1. Download OSGeo4W

Two, configure VS

3. Test code

4. Supplement: Configure QT plug-in 

5. Export the project as a template

6. Solving a problem in Release mode


Since recompiling QGIS is still difficult for beginners, here is another method for secondary development without compiling QGIS, without downloading the entire source code of QGIS.

1. Download OSGeo4W

https://download.osgeo.org/osgeo4w/v2/osgeo4w-setup.exe

Then search for qgis-ltr, which means the long-term support version. Of course, you can also choose other versions of qgis.

In fact, the above qgis desktop is not needed for secondary development, but it is recommended to install it in order to understand the functions of qgis in detail. After installation, you can open the qgis software.

 Then click on the next page to install, pay attention to install the recommended dependencies together.

 The installation is completed as shown in the figure above.

Two, configure VS

I chose the VS2017 version, the specific version requirements can refer to the official document

 Note here: I see that the tutorials written by other students need to install QT locally, and then configure the QT plug-in for VS to add the locally installed QT environment. Again, this is not necessary, you can do it, but it is recommended to use the QT environment that comes with QGIS for development.

[OSGeo4W] below means the folder where you installed OSGeo4W locally.

(1) Create a new C++ empty project, add a source file with any name.

(2) Open the project properties and add the following directories to the "Include Directories" of the VC++ directory

[OSGeo4W]\apps\Qt5\include
[OSGeo4W]\apps\Qt5\include\QtCore
[OSGeo4W]\apps\Qt5\include\QtGui
[OSGeo4W]\apps\Qt5\include\QtXml
[OSGeo4W]\apps\Qt5\include\QtWidgets
[OSGeo4W]\apps\qgis-ltr\include

In OSGeo4W, all installed libraries are stored in  apps folders by category. [OSGeo4W]\apps\Qt5\include The header files of different modules of Qt5 are stored below, which are stored in folders. In addition to the four basic modules I mentioned above, they can be added as needed.

(3) Add the following directories to the "Library Directory" of the VC++ directory

[OSGeo4W]\apps\Qt5\lib
[OSGeo4W]\apps\qgis-ltr\lib

(4) In the project property page, select "Linker", "Input", and add in "Additional Dependencies"

Qt5Core.lib
Qt5Gui.lib
Qt5Widgets.lib
qgis_core.lib
qgis_gui.lib

 (5) Setting up the preprocessor

Fill in C/C++, preprocessor, preprocessing definition

_USE_MATH_DEFINES

(6) Setting the C++ language standard

In C/C++, Language, C++ Language Standard, set to ISO C++17 Standard (/std:c++17)

(7) Set code

In C/C++, all options, additional options, set to /utf-8

(8) Set up the debugging environment

Select "Debug" in the project property page, and set the value of "Environment" to

PATH=D:\OSGeo4W\bin;D:\OSGeo4W\apps\qgis-ltr\bin;D:\OSGeo4W\apps\Qt5\bin

The purpose of this step is to enable the program to correctly obtain the DLL dynamic library required for runtime during the debugging process.

3. Test code

Add the following code to the newly created cpp file

#include <qgsapplication.h>

int main(int argc, char **argv)
{
    // 创建 QgsApplication 实例
	QgsApplication app(argc, argv, true);
}

Successfully run! Congratulations, you have completed the environment configuration for the secondary development of QGIS!

4. Supplement: Configure QT plug-in 

Qt-based programs need the support of various Qt plug-ins (such as picture format plug-ins, database plug-ins, operating platform plug-ins, etc.) at runtime. [OSGeo4W]\apps\Qt5\plugins These plug-in files also exist in the folder as DLLs  . If a specific situation is encountered during the running of the program, these plug-ins will be dynamically loaded (such as required for loading .jpg images  qjpeg.dll). So we have to tell the Qt program where the plugin folder is. In addition to copying the contents of the plugins folder directly to the same directory as the executable file, we can use  qt.conf configuration files. Create a file in the same directory as the executable file  qt.conf and enter the following content

[Paths]
Plugins=D:/OSGeo4W/apps/Qt5/plugins

as the picture shows

 

5. Export the project as a template

After exporting the project as a template, we can choose this template when we create a new project, so we don't need to spend time configuring the environment, but the QT plug-in and debugging environment still need to be configured again!

export template

 Just go to the next step

 Create a new project using a template

6. Solving a problem in Release mode

In order to make the exported project template more convenient to use, I configured the project properties of Debug mode and Release mode separately, but the project that can run normally in Debug mode, but in Release mode reports fatal error LNK1000: Internal error during IMAGE::BuildImage

The solution is: in the general configuration properties, whole program optimization, set to no whole program optimization

Guess you like

Origin blog.csdn.net/KK_2018/article/details/132161333