Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C2491 Error using QT_INIT_METAOBJECT
Forum Updated to NodeBB v4.3 + New Features

C2491 Error using QT_INIT_METAOBJECT

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DWayneD Offline
    DWayneD Offline
    DWayne
    wrote on last edited by DWayne
    #1

    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 = { {
        &QWidget::staticMetaObject,
        qt_meta_stringdata_SpinboxSlider.data,
        qt_meta_data_SpinboxSlider,
        qt_static_metacall,
        qt_meta_extradata_SpinboxSlider,
        nullptr
    } };
    
    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      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?

      1 Reply Last reply
      0
      • DWayneD DWayne

        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 = { {
            &QWidget::staticMetaObject,
            qt_meta_stringdata_SpinboxSlider.data,
            qt_meta_data_SpinboxSlider,
            qt_static_metacall,
            qt_meta_extradata_SpinboxSlider,
            nullptr
        } };
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • DWayneD Offline
          DWayneD Offline
          DWayne
          wrote on last edited by
          #4

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

          class QDESIGNER_WIDGET_EXPORT SpinboxSlider : public QWidget
          {
              Q_OBJECT
          
              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.

          kshegunovK 1 Reply Last reply
          0
          • DWayneD DWayne

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

            class QDESIGNER_WIDGET_EXPORT SpinboxSlider : public QWidget
            {
                Q_OBJECT
            
                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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @DWayne said in C2491 Error using QT_INIT_METAOBJECT:

            class QDESIGNER_WIDGET_EXPORT SpinboxSlider : public QWidget
            {
                Q_OBJECT
            

            [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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • DWayneD Offline
                DWayneD Offline
                DWayne
                wrote on last edited by
                #7

                Could this be related?
                error2.PNG

                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

                Q_PLUGIN_METADATA(cust_widgets)
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Did you read the documentation I linked to ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • DWayneD Offline
                    DWayneD Offline
                    DWayne
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • DWayneD Offline
                      DWayneD Offline
                      DWayne
                      wrote on last edited by
                      #10

                      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")
                          target_compile_definitions(${target_name}
                            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)>")
                        else()
                          target_compile_definitions(${target_name}
                            PUBLIC     "${declspec_def}=")
                        endif()
                      endfunction()
                      
                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved