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. Undefined reference to VTable when using cmake
QtWS25 Last Chance

Undefined reference to VTable when using cmake

Scheduled Pinned Locked Moved General and Desktop
qt6cmakemsys2linker errors
7 Posts 3 Posters 654 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.
  • G Offline
    G Offline
    gahjouyooj
    wrote on 5 Jan 2025, 04:30 last edited by
    #1

    When I attempt to compile a program, it causes a linker error, and the usual fixes aren't working.

    CMakeFiles/aoeu.dir/src/reader.cc.obj:reader.cc:(.rdata$.refptr._ZTV6reader[.refptr._ZTV6reader]+0x0): undefined reference to `vtable for reader'
    

    When I try to include reader.moc, I get an include error. When I move everything to main.cc, and compile it there, it works. I do intend to use slots and stuff in my class, so I do need to include Q_OBJECT.

    I'm using MSYS2, g++ and ld.

    CMakeLists.txt

    # Standards and versions  
    cmake_minimum_required(VERSION 3.30.0)
    set(CMAKE_CXX_STANDARD_REQUIRED 17)
    
    # Metadata 
    project(aoeu VERSION 0.0.0 LANGUAGES CXX)
    set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
    set(CMAKE_AUTOMOC ON)
    
    # Packages 
    
    find_package(Qt6 COMPONENTS Widgets REQUIRED)
    
    file(GLOB_RECURSE SOURCES ${PROJECT_SOURCE_DIR}/src/*.cc)
    
    add_executable(${PROJECT_NAME} ${SOURCES})
    
    # Pch files
    target_precompile_headers(${PROJECT_NAME} PRIVATE <QtWidgets>)
    
    target_include_directories(${PROJECT_NAME} PRIVATE "${PROJECT_SOURCE_DIR}/include")
    
    target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Widgets)
    

    main.cc

    #include <QtWidgets>
    
    #include "reader.hh"
    
    int main(int argc, char **argv) {
      QApplication app(argc, argv);
    
      QWidget window;
      window.show();
    
      // Placeholder for actual text previewing 
      reader textBox("boeu", &window);
    
      return app.exec();
    }
    

    reader.hh

    #if !defined(READER_HH)
    #define READER_HH
    
    #include <QPlainTextEdit>
    
    class reader : public QPlainTextEdit {
      Q_OBJECT
    
    public:
      reader(QString text, QWidget *parent = nullptr);
    
      virtual ~reader();
    };
    
    // `fatal error: reader.moc: No such file or directory` if uncommented 
    // #include "reader.moc"
    
    #endif
    

    reader.cc

    #include "reader.hh"
    
    reader::reader(QString text, QWidget *parent) : QPlainTextEdit(text, parent) {
      setReadOnly(true);
    }
    
    reader::~reader() {}
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 5 Jan 2025, 05:36 last edited by
      #2

      The root cause of this problem is the separation of C++ source files from header files. The separation hides reader.hh from AUTOMOC and results in no generated code for this class.
      Placing all of these sources and includes in the same directory links correctly.

      You can repair this by adding the header files to the add_executable() calls beside the SOURCES. There may be other/better ways to achieve this.

      G 1 Reply Last reply 5 Jan 2025, 15:12
      2
      • C ChrisW67
        5 Jan 2025, 05:36

        The root cause of this problem is the separation of C++ source files from header files. The separation hides reader.hh from AUTOMOC and results in no generated code for this class.
        Placing all of these sources and includes in the same directory links correctly.

        You can repair this by adding the header files to the add_executable() calls beside the SOURCES. There may be other/better ways to achieve this.

        G Offline
        G Offline
        gahjouyooj
        wrote on 5 Jan 2025, 15:12 last edited by
        #3

        @ChrisW67 said in Undefined reference to VTable when using cmake:

        The root cause of this problem is the separation of C++ source files from header files. The separation hides reader.hh from AUTOMOC and results in no generated code for this class.
        Placing all of these sources and includes in the same directory links correctly.

        You can repair this by adding the header files to the add_executable() calls beside the SOURCES. There may be other/better ways to achieve this.

        Sorry, this has not helped the issue. When including reader.moc, it still fails to run the moc. In addition, when removing the reader.moc include, I had another linker error.

        multiple definition of `reader::staticMetaObject'; CMakeFiles/aoeu.dir/aoeu_autogen/mocs_compilation.cpp.obj:U:/repos/novelreader/build/aoeu_autogen/UVLADIE3JM/moc_reader.cpp:63: first defined here
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 5 Jan 2025, 15:20 last edited by
          #4

          You have to include the header, not the generated moc file

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • G Offline
            G Offline
            gahjouyooj
            wrote on 6 Jan 2025, 08:33 last edited by
            #5

            I see. I've managed to fix the issues.

            Changes I've made to solve it:

            1. Added .hh files to sources.
            2. Removed #include "reader.moc".

            Just to clarify, when do you need to use #include "file.moc"? It's been suggested as a solution to the vtable include error by many people.

            1 Reply Last reply
            0
            • G gahjouyooj marked this topic as a regular topic on 6 Jan 2025, 08:34
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 6 Jan 2025, 11:01 last edited by
              #6

              You only have to include the moc file in the source file when you declared a qobject derived class in the source instead the header.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • G Offline
                G Offline
                gahjouyooj
                wrote on 6 Jan 2025, 11:45 last edited by
                #7

                I see, thanks for explaining.

                1 Reply Last reply
                0

                1/7

                5 Jan 2025, 04:30

                • Login

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