繁体   English   中英

CLion 和 Qt5:调整 qmake

[英]CLion and Qt5: tuning qmake

我有一个在别处解释的设置,例如这里 我在系统范围内安装了Qt5 ,并且在我的CMakeLists.txt有必要的行。 我的 IDE 是 Clion。 在我添加Q_OBJECT宏之前,简单 GUI 中的一切都很好(我希望它可以将信号连接到插槽)。 现在,当我这样做时,我得到了undefined reference to vtable类型错误的undefined reference to vtable ,这在网上也有很多。 我的困惑源于这样一个事实,即有些人建议为您的项目使用Qt5捆绑的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)

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

编辑:帮助我的是Qt 5 cmake 中提到的qt5_wrapper_cpp事情失败,未定义引用 hello world 上的 vtable,以 inc 和 src 作为子目录

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)应该可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM