我有其他地方介绍的设置,例如here。我在系统范围内安装了Qt5
,并在我的CMakeLists.txt
中添加必要的行。我的IDE是Clion。在我添加Q_OBJECT
宏之前,简单的GUI中的所有功能都可以正常运行(我希望这将信号连接至插槽)。现在,当我这样做时,我得到了undefined reference to vtable
型错误,也可以在网上找到大量。我感到困惑的是,有人建议在您的项目中使用Qt5
捆绑cmake
,从本质上讲,这意味着“仅用于GUI”,我需要更改工具链。但是实际上,有人对此什么也没说。所有人都说那是>>
每次添加/删除Q_OBJECT时Qt运行qmake
现在,如何在我的
CMakeLists.txt
中捕获它? -其相关部分如下。我在moc
里面看到了qmake
和/usr/lib/qt5/bin
;那么如何将此传达给CLion
?
# ----- GUI part ----- # Qt5 inclusion # The meta object compiler is one of the core functionality of Qt, it reads a C++ header file and if it finds a # Q_OBJECT macro, it will produces a C++ source file containing meta object code for the class. # It's the mechanism that allow signal and slots to work. set(CMAKE_INCLUDE_CURRENT_DIR ON) # set(CMAKE_PREFIX_PATH $ENV{QT_DIR}/$ENV{QT_VERSION}/gcc_64/lib/cmake) set(CMAKE_PREFIX_PATH /usr/lib/qt5/bin/) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) # Enable user interface compiler (UIC) # The user interface compiler is a program that read XML from the .ui file # generated by Qt Interface Designer and generate C++ code from it. set(CMAKE_AUTOUIC ON) if(CMAKE_VERSION VERSION_LESS "3.7.0") set(CMAKE_INCLUDE_CURRENT_DIR ON) endif() set(CMAKE_MODULE_PATH /usr/lib/qt5) # @see: https://stackoverflow.com/questions/51994603/cmake-qt5-undefined-reference-to-qprinterqprinterqprinterprintermode SET(QT5_MODULES Widgets PrintSupport) find_package(Qt5 COMPONENTS ${QT5_MODULES} REQUIRED) add_subdirectory(${PROJECT_SOURCE_DIR}/extern/qcustomplot) add_executable(gui ${PROJECT_SOURCE_DIR}/gui/main.cpp ${PROJECT_SOURCE_DIR}/extern/qcustomplot/qcustomplot.cpp) set_target_properties(gui PROPERTIES LINKER_LANGUAGE CXX) target_link_libraries(gui PUBLIC Qt5::Core Qt5::Widgets qcustomplot)
不要介意冗长的注释;我最初的GUI培训是在
Java Swing
中,我发现它们很有用。
编辑:帮助我的是qt5_wrapper_cpp
Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs
我有其他地方介绍的设置,例如这里。我在系统范围内安装了Qt5,并且在CMakeLists.txt中具有必要的行。我的IDE是Clion。直到我...
Q_OBJECT
宏需要代码生成。这就是为什么您有undefined reference
个例外。我不记得确切如何为Qt配置cmake项目,但我建议阅读https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html。