CLion和Qt5:调整qmake

我有一个设置,如其他地方所述。 here。我已经在系统范围内安装了Qt5, 并在我的CMakeLists.txt中包含必要的行。我的IDE是Clion。 在我添加Q_OBJECT宏之前,简单的GUI中的所有内容都可以正常运行(我希望这可以将信号连接到插槽)。现在,当我这样做时,我得到了undefined reference to vtable型错误, 也可以在网上找到大量。 我之所以感到困惑,是因为有人建议在您的项目中使用Qt5捆绑的cmake, 从本质上讲,这意味着“仅用于GUI”,我需要更改工具链。但是有些人对此一无所知。所有人都在说

  

Qt每次添加/删除Q_OBJECT都运行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)

不要介意详细的评论;我最初在Java Swing中进行GUI培训时,发现它们很有用。

编辑:对我有帮助的是其中提到的qt5_wrapper_cpp Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs

xzsytc 回答:CLion和Qt5:调整qmake

Q_OBJECT宏需要代码生成。这就是为什么您有undefined reference个异常的原因。我不完全记得如何为Qt配置cmake项目,但是我建议阅读https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html

set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)之类的东西应该对您有所帮助。

本文链接:https://www.f2er.com/2917369.html

大家都在问