【问题标题】:CLion and Qt5: tuning qmakeCLion 和 Qt5:调优 qmake
【发布时间】:2019-12-15 18:44:13
【问题描述】:

我有一个在别处解释的设置,例如here。我在系统范围内安装了Qt5, 并在我的CMakeLists.txt 中有必要的行。我的 IDE 是 Clion。 在我添加 Q_OBJECT 宏之前,一个简单的 GUI 中的一切都很好(我希望它可以将信号连接到插槽)。现在,当我这样做时,我得到undefined reference to vtable-type 错误, 在网上也可以找到大量的。 我的困惑源于一些人建议在您的项目中使用Qt5-bundled cmake, 这本质上意味着“仅用于 GUI”我需要更改工具链。但有些人实际上对此只字未提。都说是这样的

每次添加/删除 Q_OBJECT 时,Qt 都会运行 qmake

现在,如何在我的 CMakeLists.txt 中捕获它? ——相关部分如下。 我在/usr/lib/qt5/bin 中看到了mocqmake;那么如何与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)

别管冗长的 cmets;我最初的 GUI 培训是在 Java Swing,我发现它们很有用。

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

【问题讨论】:

    标签: c++ qt cmake qt5 clion


    【解决方案1】:

    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) 这样的东西应该可以帮助你。

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多