简体   繁体   中英

QGIS with standalone C++ application in Qt Creator

My projects consists of several subprojects, one of which includes QGIS libraries.

.pro file :

TEMPLATE = subdirs

CONFIG += ordered

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

This is the .pro file of Maps, that includes the QGIS libraries:

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)

This is the .pro file for SensorNetworkUI:

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

However, I am getting errors like these:

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)

I also noticed that the auto complete suggestions given by Qt Creator don't include the QGIS header files. Does this mean Qt Creator is not able to find them? It does not throw up errors once the entire name has been typed out. I can even open said header files in Qt Creator.

Can someone please tell me where I am going wrong?

EDIT: This is where the error begins:

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"

Your error definitely points to the linker not finding the correct symbol in the library, as indicated by "error LNK", so headers were correctly found previously.

  1. The core library of QGIS is not found
  2. The core library is not linked due to a missing dependency LIBS += ... in one of your projects
  3. The libraries were compiled with a different compiler than yours, producing an ABI that is not understood by your compiler
  4. Your headers and the actual libary differ (for instance if they point to different version QGIS).

You should probably check that the library files you point to are present and corresponds to the right version of QGIS. If linking dynamically, also make sure that all projects depending on it actually include the linker directive LIBS += ... .


EDIT: The OP found the problem to be simply the dllexport, that should be dllimport instead for QGIS CORE_EXPORT and GUI_EXPORT variables:

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

A well documented site as reference for QGIS: https://3nids.wordpress.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM