[英]CLion and Qt5: tuning qmake
I have a set-up as explained elsewhere, eg here .我有一个在别处解释的设置,例如这里。 I have Qt5
installed system-wide, and have the requisite lines in my CMakeLists.txt
.我在系统范围内安装了Qt5
,并且在我的CMakeLists.txt
有必要的行。 My IDE is Clion.我的 IDE 是 Clion。 Everything in a simple GUI goes fine until I add Q_OBJECT
macro (I want this to connect signals to slots).在我添加Q_OBJECT
宏之前,简单 GUI 中的一切都很好(我希望它可以将信号连接到插槽)。 Now, when I do this, I get the undefined reference to vtable
-type error, that is also found in abundance on the net.现在,当我这样做时,我得到了undefined reference to vtable
类型错误的undefined reference to vtable
,这在网上也有很多。 My confusion arises from the fact that some recommend using Qt5
-bundled cmake
for your project, which essentially means "just for GUI" I need to change the toolchain.我的困惑源于这样一个事实,即有些人建议为您的项目使用Qt5
捆绑的cmake
,这实质上意味着“仅用于 GUI”,我需要更改工具链。 But some actually say nothing about it.但有些人实际上什么也没说。 What all say is that都说的是
Qt runs qmake every time Q_OBJECT is added/deleted每次添加/删除 Q_OBJECT 时,Qt 都会运行 qmake
Now, how to capture that in my CMakeLists.txt
?现在,如何在我的CMakeLists.txt
捕获它? -- the relevant part thereof is given below. ——相关部分如下。 I've seen moc
and qmake
inside /usr/lib/qt5/bin
;我在/usr/lib/qt5/bin
看到了moc
和qmake
; so how to communicate this to CLion
?那么如何将其传达给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)
Never mind the verbose comments;别介意冗长的评论; my initial GUI training being in Java Swing
, I found them useful.我最初的 GUI 培训是在Java Swing
,我发现它们很有用。
EDIT: what helped me was the qt5_wrapper_cpp
thing mentioned in Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs编辑:帮助我的是Qt 5 cmake 中提到的qt5_wrapper_cpp
事情失败,未定义引用 hello world 上的 vtable,以 inc 和 src 作为子目录
Q_OBJECT
macro requires code generation. Q_OBJECT
宏需要代码生成。 That's why you got undefined reference
exceptions.这就是为什么您会遇到undefined reference
异常。 I don't remember exactly how to configure a cmake project for Qt, but I would recommend reading https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html .我不记得如何为 Qt 配置 cmake 项目,但我建议阅读https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html 。
Something like set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)
should help you.像set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)
应该可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.