QGIS 与 Qt Creator 中的独立 C++ 应用程序

QGIS with standalone C++ application in Qt Creator

我的项目由几个子项目组成,其中之一包括 QGIS 库。

.pro 文件:

TEMPLATE = subdirs

CONFIG += ordered

SUBDIRS += \
    Utils \
    Maps \
    GoogleMapsInterface \   
    Positioning \
    NetworkObjects \
    DataBaseManager \
    SensorNetworkUI \

这是地图的 .pro 文件,其中包含 QGIS 库:

CONFIG += c++11
CONFIG += staticlib
TEMPLATE = lib
QT += core gui
QT += widgets
QT += xml

TARGET = maps

DEFINES += MAPS_INTERFACE

INCLUDEPATH += "C:/OSGeo4W/apps/qgis/include/"
INCLUDEPATH += "C:/OSGeo4W/include/"

win32:CONFIG(Release, Debug|Release) {
 LIBS += -LC:/OSGeo4W/lib/ -lQtCore4
 LIBS += -LC:/OSGeo4W/lib/ -lQtGui4
 LIBS += -LC:/OSGeo4W/lib/ -lQtXml4
}

else:win32:CONFIG(Debug, Debug|Release) {
 PRE_TARGETDEPS += C:/OSGeo4W/lib/QtCored4.lib
 PRE_TARGETDEPS += C:/OSGeo4W/lib/QtGuid4.lib
 PRE_TARGETDEPS += C:/OSGeo4W/lib/QtXmld4.lib
 LIBS += -LC:/OSGeo4W/lib/ -lQtCored4
 LIBS += -LC:/OSGeo4W/lib/ -lQtGuid4
 LIBS += -LC:/OSGeo4W/lib/ -lQtXmld4
}

LIBS += -LC:/OSGeo4W/lib -lgdal_i -lgeos_c
LIBS += -LC:/OSGeo4W/apps/qgis-dev/lib -lqgis_core -lqgis_gui

DEFINES += CORE_EXPORT=__declspec(dllexport)
DEFINES += GUI_EXPORT=__declspec(dllexport)

这是 SensorNetworkUI 的 .pro 文件:

CONFIG += console
TARGET = SensorNetwork
TEMPLATE = app

greaterThan(QT_MAJOR_VERSION, 4):
QT += widgets
QT += core gui
QT += opengl
QT += sql
QT += xml
QT += multimedia

INCLUDEPATH += ../
INCLUDEPATH += "C:/OSGeo4W/apps/qgis/include/"
INCLUDEPATH += "C:/OSGeo4W/include/"

win32:CONFIG(Release, Debug|Release) {
 LIBS += -LC:/OSGeo4W/lib/ -lQtCore4
 LIBS += -LC:/OSGeo4W/lib/ -lQtGui4
 LIBS += -LC:/OSGeo4W/lib/ -lQtXml4
}

else:win32:CONFIG(Debug, Debug|Release) {
 PRE_TARGETDEPS += C:/OSGeo4W/lib/QtCored4.lib
 PRE_TARGETDEPS += C:/OSGeo4W/lib/QtGuid4.lib
 PRE_TARGETDEPS += C:/OSGeo4W/lib/QtXmld4.lib
 LIBS += -LC:/OSGeo4W/lib/ -lQtCored4
 LIBS += -LC:/OSGeo4W/lib/ -lQtGuid4
 LIBS += -LC:/OSGeo4W/lib/ -lQtXmld4
}

DEFINES += CORE_EXPORT=__declspec(dllexport)
DEFINES += GUI_EXPORT=__declspec(dllexport)

LIBS += -LC:/OSGeo4W/lib -lgdal_i -lgeos_c
LIBS += -LC:/OSGeo4W/apps/qgis-dev/lib -lqgis_core -lqgis_gui
LIBS += -L../ -ldbmanager -lsnobjects -lpositioning -lsnutils -lmaps

但是,我收到如下错误:

maps.lib(moc_ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)
MainWindow.obj : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)

我还注意到 Qt Creator 给出的自动完成建议不包括 QGIS 头文件。这是否意味着 Qt Creator 无法找到它们?一旦输入了整个名称,它就不会抛出错误。我什至可以在 Qt Creator 中打开上述头文件。

谁能告诉我哪里出错了?

编辑: 这是错误开始的地方:

Creating library ..\SensorNetwork.lib and object ..\SensorNetwork.exp
maps.lib(moc_ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)
MainWindow.obj : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)
maps.lib(MapCanvas.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)
maps.lib(ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)
maps.lib(moc_MapCanvas.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsFeature const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsFeature@@PAV2@@Z)
maps.lib(moc_ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsExpressionContext const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsExpressionContext@@PAV2@@Z)
MainWindow.obj : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsExpressionContext const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsExpressionContext@@PAV2@@Z)
maps.lib(MapCanvas.obj) : error LNK2001: unresolved external symbol "public: virtual class QVariant __thiscall QgsExpression::Function::func(class QList<class QVariant> const &,class QgsExpressionContext const *,class QgsExpression *)" (?func@Function@QgsExpression@@UAE?AVQVariant@@ABV?$QList@VQVariant@@@@PBVQgsExpressionContext@@PAV2@@Z)
...
...
maps.lib(MapCanvas.obj) : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QgsRasterDataProvider::staticMetaObject" (?staticMetaObject@QgsRasterDataProvider@@2UQMetaObject@@B)
maps.lib(MapCanvas.obj) : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QgsRasterLayer::staticMetaObject" (?staticMetaObject@QgsRasterLayer@@2UQMetaObject@@B)
maps.lib(ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual void __thiscall QgsMapTool::renderComplete(void)" (?renderComplete@QgsMapTool@@UAEXXZ)
maps.lib(ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QgsMapTool::isTransient(void)const " (?isTransient@QgsMapTool@@UBE_NXZ)
maps.lib(ClickTool.obj) : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QgsMapTool::isEditTool(void)const " (?isEditTool@QgsMapTool@@UBE_NXZ)
..\SensorNetwork.exe : fatal error LNK1120: 93 unresolved externals
jom: D:\Atmika\Sensor_Network\build\win32\Debug\SensorNetworkUI\Makefile.Debug [..\SensorNetwork.exe] Error 1120
jom: D:\Atmika\Sensor_Network\build\win32\Debug\SensorNetworkUI\Makefile [debug] Error 2
14:21:50: The process "D:\Qt\Qt5.7.1\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project Sensor_Network (kit: Desktop Qt 5.7.1 MSVC2015 32bit)
When executing step "Make"

您的错误肯定指向链接器未在库中找到正确的符号,如 "error LNK" 所示,因此之前已正确找到 headers。

  1. 找不到QGIS核心库
  2. 由于您的一个项目中缺少依赖项LIBS += ...,核心库未链接
  3. 这些库是使用与您不同的编译器编译的,产生了您的编译器无法理解的 ABI
  4. 您的 headers 和实际的库不同(例如,如果它们指向不同版本的 QGIS)。

您可能应该检查您指向的库文件是否存在并且对应于正确的 QGIS 版本。 如果动态链接,还要确保依赖于它的 all 项目实际包含链接器指令 LIBS += ....


编辑:OP 发现问题只是 dllexport,对于 QGIS CORE_EXPORT 和 GUI_EXPORT 变量,应该是 dllimport

DEFINES += CORE_EXPORT=__declspec(dllimport)
DEFINES += GUI_EXPORT=__declspec(dllimport)

一个有据可查的网站,作为 QGIS 的参考: https://3nids.wordpress.com/