Using QGIS API but getting unresolved external symbol "public: static struct QMetaObject const in Windows

StackOverflow https://stackoverflow.com/questions/11923478

  •  25-06-2021
  •  | 
  •  

Question

I am trying to build an application that uses the QT and the QGIS API (1.8.0). Under Linux I don't get any problem (using QT 4.7.0), but under Windows (using QT 4.8.1) I get:

1>maptoolselect.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QgsVectorLayer::staticMetaObject" (?staticMetaObject@QgsVectorLayer@@2UQMetaObject@@B)
1>mapwidget.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QgsVectorLayer::staticMetaObject" (?staticMetaObject@QgsVectorLayer@@2UQMetaObject@@B)
1>mapwidget.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QgsRasterLayer::staticMetaObject" (?staticMetaObject@QgsRasterLayer@@2UQMetaObject@@B)
1>debug\\nile.exe : fatal error LNK1120: 2 unresolved externals

My .pro file is as follows:

QT       += core gui sql xml

TARGET = nile
TEMPLATE = app

unix:INCLUDEPATH += /usr/local/gis/include/qgis /usr/local/gis/include ./mapviewsrc
win32:INCLUDEPATH += C:/QGIS/apps/qgis/include C:/QGIS/include ./mapviewsrc

unix:LIBS += -L/usr/local/gis/lib -lgdal -lgeos -lqgis_core -lqgis_gui
win32:LIBS += C:/QGIS/apps/qgis/lib/qgis_core.lib C:/QGIS/apps/qgis/lib/qgis_gui.lib C:/QGIS/lib/geos_c_i.lib C:/QGIS/lib/gdal_i.lib

CONFIG += release

DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} CORE_EXPORT= GUI_EXPORT=

Both classes maptoolselect and mapwidget that uses QgsVectorLayer and QgsRasterLayer have Q_OBJECT in their class definition.

Any help is much appreciated.

Many thanks, Carlos

Was it helpful?

Solution

It turns out that the error happens because of the use of qobject_cast in lines like:

QgsVectorLayer* vlayer = qobject_cast( mCanvas->currentLayer() );

If I change those lines to use dynamic_cast the error disappears and the application runs properly.

However I have no idea why I can use qobject_cast in Linux but not in Windows!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top