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. CMake add_library: error LNK2001: unresolved external symbol "struct QMetaObject const myClass::staticMetaObject"
Forum Update on Monday, May 27th 2025

CMake add_library: error LNK2001: unresolved external symbol "struct QMetaObject const myClass::staticMetaObject"

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.5k Views
  • 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.
  • tracymaT Offline
    tracymaT Offline
    tracyma
    wrote on last edited by tracyma
    #1

    Get link error when build dll library: 1>animationitem.obj : error LNK2001: unresolved external symbol "struct QMetaObject const myclass::staticMetaObject" (?staticMetaObject@myclass@@3UQMetaObject@@B).

    moc_animationitem.cpp is actually generated. I have no idea what's wrong.

    VS project is generated from CMake.

    Qt 5.12.0
    VS 2017 x86

    cmake_minimum_required(VERSION 3.10.0)
    
    project(libMyClass)
    
    add_subdirectory( 7zip )
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_AUTOUIC ON)
    
    find_package(Qt5 COMPONENTS Core Gui Widgets Quick Qml QuickControls2 WebEngineWidgets WebView WebChannel Svg Xml Multimedia REQUIRED)
    
    qt5_add_resources( QT_RESOURCES resource.qrc )
    
    add_library( libMyClass
        SHARED
            # and others ...
            items/animationitem.cpp
    )
    
    target_compile_definitions( libMyClass PRIVATE ITEM_MODE_VIEW BUILD_XESLIDE_DLL )
    #target_compile_options( libMyClass PRIVATE -Wall )
    target_compile_features( libMyClass PRIVATE cxx_std_17 )
    
    target_include_directories( libMyClass
        PRIVATE 
            .
            7zip/win/CPP 
            7zip/win/C
            player/player_widgets
            UI
    )
    
    target_link_libraries( libMyClass 
        PUBLIC 
            Qt5::Core 
            Qt5::Gui
            Qt5::Widgets 
            Qt5::Quick 
            Qt5::Qml 
            Qt5::QuickControls2 
            Qt5::WebEngineWidgets 
            Qt5::WebView 
            Qt5::WebChannel 
            Qt5::Svg 
            Qt5::Xml 
            Qt5::Multimedia
            libArchive
            user32.lib
    )
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Based on the cmake-qt documentation, it seems you have to include the corresponding generated files at the bottom of your .cpp files.

      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
      0
      • tracymaT Offline
        tracymaT Offline
        tracyma
        wrote on last edited by tracyma
        #3

        have tried, do not work. : <

        AnimationItem is QQuickItem-derived , how to deal with custom item in dll ? should i qmlRegisterType the item somewhere ?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4
          1. qt5_add_resources is obsolete. if you set(CMAKE_AUTORCC ON) just add your .qrc files as they were .cpp to add_library/add_executable
          2. moc needs the headers, make sure CMake can see them, usually you can use set(CMAKE_INCLUDE_CURRENT_DIR ON)

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          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