Why am I getting “undefined reference to vtable…” errors when linking this Qt 5.0 application?

前端 未结 3 729
执笔经年
执笔经年 2021-01-11 13:44

I\'ve got a relatively simple Qt 5.0 project that uses CMake 2.8.9:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_INCLUDE_CURRENT_D         


        
相关标签:
3条回答
  • 2021-01-11 14:09

    I also ran into this problem yesterday and the above mentioned answers did't help. I already used set (CMAKE_AUTOMOC ON) and also qt5_wrap_cpp.

    I tried to remember what I did, because I had a working version but it stopped working after "some" changes. I finally remembered that I tried to split the include files into a separate directory hierarchy. After reverting that and putting the include files back into the CMakeLists.txt it worked again. I sure don't know why, and I would like to know what went wrong, but I settled now for keeping the includes near the cpp files.

    set(SOURCES
        buffer.h
        ITVSet.h
        MainWindow.h
        MainWindow.cpp
        TVSet.h
        TVSet.cpp
    )
    
    0 讨论(0)
  • 2021-01-11 14:10

    I struggled with this for a long time using all the hints published here:

    http://doc.qt.io/qt-5/cmake-manual.html

    And here

    https://www.kdab.com/using-cmake-with-qt-5/

    What I had to do was specify things in the right order. For example, the following is the top of my CMakeLists.txt. Note that the two CMAKE set directives come before add_executable. Once I did this, I was able to link without undefined symbols and vtable references. I just thought I'd post this for the benefit of others.

    cmake_minimum_required (VERSION 2.8)
    
    set (CMAKE_AUTOMOC ON)
    set (CMAKE_INCLUDE_CURRENT_DIR ON)
    add_executable(FHSpectrumSensor wideband_seq_spectrum_sensor.cpp sensor.cpp   gui.cpp ${gui_SRC})
    

    Later in the CMakeLists.txt I have the following:

     find_package(Qt5Widgets REQUIRED)
     find_package(Qt5Charts REQUIRED)
     find_package(Qt5Core REQUIRED)
    
     qt5_use_modules(FHSpectrumSensor Widgets Charts)
     qt5_wrap_cpp(gui_SRC gui.h gui.cpp)
    

    That did the trick.

    0 讨论(0)
  • 2021-01-11 14:16

    Turns out I forgot:

    set(CMAKE_AUTOMOC ON)
    

    At the top of the CMakeLists.txt file.

    0 讨论(0)
提交回复
热议问题