Important: Please read the Qt Code of Conduct -

C2491 Error using QT_INIT_METAOBJECT

  • Hello,

    I am using Qt 5.13.2 (porting from Qt 4.8.2), CMAke 3.16.0-rc3, VS2017 and running into the following errors:

    Severity	Code	Description	Project	File	Line
    Error	C2491	'SpinboxSlider::staticMetaObject': definition of dllimport static data member not allowed	cust_widgets	moc_SpinboxSlider.cpp	229

    Offending code in moc_SpinboxSlider.cpp

    QT_INIT_METAOBJECT const QMetaObject SpinboxSlider::staticMetaObject = { {
    } };

  • Lifetime Qt Champion

    I assume its not an old MOC file but newly generated. :)

    Is SpinboxSlider.cpp / h decorated with import/export or similar for being used
    in a library?

  • Moderators

    You have a __declspec(dllimport) somewhere where it shouldn't appear at all. Please show the declaration of SpinboxSlider and the macro which exports/imports it from a library.

  • Yes, newly generated MOC. Here is the top of the header:

    class QDESIGNER_WIDGET_EXPORT SpinboxSlider : public QWidget
        Q_ENUMS( TickSetting )
        Q_PROPERTY( int minValue READ minValue WRITE setMinValue )
        Q_PROPERTY( int maxValue READ maxValue WRITE setMaxValue )
        Q_PROPERTY( int lineStep READ lineStep WRITE setLineStep )
        Q_PROPERTY( int pageStep READ pageStep WRITE setPageStep )
        Q_PROPERTY( int value READ value WRITE setValue )
        Q_PROPERTY( Qt::Orientation orientation READ orientation WRITE setOrientation )
        Q_PROPERTY( int tickInterval READ tickInterval WRITE setTickInterval )
        Q_PROPERTY( QString spinboxLabel READ spinboxLabel WRITE setSpinboxLabel )
        Q_PROPERTY( QSlider::TickPosition tickmarks READ tickmarks WRITE setTickmarks )

    Another important difference of note is that i'm now using CMake instead of QMake.

  • Moderators

    @DWayne said in C2491 Error using QT_INIT_METAOBJECT:

    class QDESIGNER_WIDGET_EXPORT SpinboxSlider : public QWidget

    [more code]

    QDESIGNER_WIDGET_EXPORT is your problem. Why is this there and how it came to be?

    Another important difference of note is that i'm now using CMake instead of QMake.

    Nope, it's unrelated to the build system. The error you get is from the link step.

  • Lifetime Qt Champion


    Are you properly defining QDESIGNER_WIDGET_EXPORT ?

    Note that since these are your classes, you should have your own macros for that as shown here.

  • Could this be related?

    I do not know if i'm properly defining QDESIGNER_WIDGET_EXPORT

    Here is a line of code that complained when I compiled

    Q_EXPORT_PLUGIN2(cust_widgets, CustomWidgets) 

    I changed it to


  • Lifetime Qt Champion

    Did you read the documentation I linked to ?

  • I will look over now and get back to you. thanks

  • SGaist,

    thanks for the article. I've replaced QDESIGNER_WIDGET_EXPORT with a CMake module that contains the following for use in targets that need to do exports:

    cmake_minimum_required(VERSION 3.16)
    function(SetupWindowsDllLinkage target_name declspec_def)
      if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
          PRIVATE    "${declspec_def}=$<$<STREQUAL:$<TARGET_PROPERTY:${target_name},TYPE>,SHARED_LIBRARY>:__declspec(dllexport)>"
          INTERFACE  "${declspec_def}=$<$<STREQUAL:$<TARGET_PROPERTY:${target_name},TYPE>,SHARED_LIBRARY>:__declspec(dllimport)>")
          PUBLIC     "${declspec_def}=")

Log in to reply