Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs

后端 未结 3 1793
青春惊慌失措
青春惊慌失措 2021-01-04 05:29

Update 2

After messing around a bit (and some editing of the generated Makefiles), it looks like what is happening is that moc is not properly processing Mai

3条回答
  •  情话喂你
    2021-01-04 06:30

    As noted, moc is not processing MainWindow.h in your example. One way to force this to happen is to call qt_wrap_cpp() on it directly (instead of on MainWindow.cpp) and then include the resulting file in the call to add_executable().

    Your top level CMakeLists.txt might look like:

    cmake_minimum_required(VERSION 2.8.9)
    
    #set(CMAKE_AUTOMOC ON)
    
    set(CMAKE_PREFIX_PATH "/opt/Qt/5.1.1/gcc_64")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    project(hello-world)
    
    find_package(Qt5Widgets REQUIRED)
    
    set(HW_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc)
    set(HW_GUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gui)
    
    include_directories(${HW_HEADER_DIR})
    
    subdirs(src)
    

    and your src level one like:

    qt5_wrap_cpp(hello-world_SRC ${HW_HEADER_DIR}/MainWindow.h)
    qt5_wrap_ui(hello-world_UI ${HW_GUI_DIR}/MainWindow.ui)
    
    add_executable(hello-world MainWindow.cpp main.cpp
                   ${hello-world_UI} ${hello-world_SRC})
    qt5_use_modules(hello-world Widgets)
    

    Addendum:

    • This works in your example with and without AUTOMOC enabled. I don't know for sure if having it on will cause issues in the future. If you don't enable it you'll have to manually moc any other stuff... although it might all behave like MainWindow in which case you'll be manually mocing the headers regardless.
    • You don't have to set the directory variables in the top level CMakeLists.txt but I find it cleaner than doing qt5_wrap_cpp(hello-world_SRC ../inc/MainWindow.h)
    • There might be a better way of doing this.
    • For anybody else with similar issues, so far this solution has held up in the larger project that I initially encountered this in. I will update accordingly if it fails.

提交回复
热议问题