使用 cmake 和提升的小部件编译 qt gui 时出错

Posted

技术标签:

【中文标题】使用 cmake 和提升的小部件编译 qt gui 时出错【英文标题】:error for compiling qt gui with cmake along with promoted widget 【发布时间】:2014-06-14 16:40:34 【问题描述】:

我有一个基于 ROS(catkin) 的 qt GUI 项目,它是我的 cmake

cmake_minimum_required(VERSION 2.8.0)
project(jir_seg)

find_package(catkin REQUIRED COMPONENTS qt_build roscpp)
include_directories($catkin_INCLUDE_DIRS)

catkin_package()

rosbuild_prepare_qt4(QtCore QtGui)

file(GLOB QT_FORMS RELATIVE $CMAKE_CURRENT_SOURCE_DIR ui/*.ui)
file(GLOB QT_RESOURCES RELATIVE $CMAKE_CURRENT_SOURCE_DIR resources/*.qrc)
file(GLOB_RECURSE QT_MOC RELATIVE $CMAKE_CURRENT_SOURCE_DIR FOLLOW_SYMLINKS      include/jir_seg/*.hpp)

QT4_ADD_RESOURCES(QT_RESOURCES_CPP $QT_RESOURCES)
QT4_WRAP_UI(QT_FORMS_HPP $QT_FORMS)
QT4_WRAP_CPP(QT_MOC_HPP $QT_MOC)

file(GLOB_RECURSE QT_SOURCES RELATIVE $CMAKE_CURRENT_SOURCE_DIR FOLLOW_SYMLINKS src/*.cpp)

add_executable(jir_seg $QT_SOURCES $QT_RESOURCES_CPP $QT_FORMS_HPP $QT_MOC_HPP)
target_link_libraries(jir_seg $QT_LIBRARIES $catkin_LIBRARIES )
install(TARGETS jir_seg RUNTIME DESTINATION $CATKIN_PACKAGE_BIN_DESTINATION)

它是 catkin 中自动生成的 cmake,在我的项目中,我有一个来自 qgeraphicview (JIRSEGSubClassQGV.hpp) 的子类,我向它提升了一个小部件,问题是当我编译此代码时出现此错误

In file included from /home/vahid/catkin_ws/src/jir_seg/src/../include/jir_seg  /JIRSEG2DWindow.hpp:14:0,
             from /home/vahid/catkin_ws/src/jir_seg/src/JIRSEG2DWindow.cpp:1:
/home/vahid/catkin_ws/build/jir_seg/ui_JIRSEG2DWindow.h:26:33: fatal error:   JIRSEGSubClassQGV.hpp: No such file or directory
compilation terminated.
In file included from /home/vahid/catkin_ws/src/jir_seg/src/../include/jir_seg/JIRSEG2DWindow.hpp:14:0,
             from /home/vahid/catkin_ws/src/jir_seg/src/../include/jir_seg/JIRSEGMainWindow.hpp:9,
             from /home/vahid/catkin_ws/src/jir_seg/src/JIRSEGMainWindow.cpp:1:
/home/vahid/catkin_ws/build/jir_seg/ui_JIRSEG2DWindow.h:26:33: fatal error:  JIRSEGSubClassQGV.hpp: No such file or directory
compilation terminated.
In file included from /home/vahid/catkin_ws/src/jir_seg/src/../include/jir_seg  /JIRSEG2DWindow.hpp:14:0,
             from /home/vahid/catkin_ws/src/jir_seg/src/../include/jir_seg  /JIRSEGMainWindow.hpp:9,
             from /home/vahid/catkin_ws/src/jir_seg/src/main.cpp:5:
/home/vahid/catkin_ws/build/jir_seg/ui_JIRSEG2DWindow.h:26:33: fatal error:   JIRSEGSubClassQGV.hpp: No such file or directory
compilation terminated.
make[2]: *** [jir_seg/CMakeFiles/jir_seg.dir/src/JIRSEGMainWindow.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: *** [jir_seg/CMakeFiles/jir_seg.dir/src/JIRSEG2DWindow.cpp.o] Error 1
make[2]: *** [jir_seg/CMakeFiles/jir_seg.dir/src/main.cpp.o] Error 1
make[1]: *** [jir_seg/CMakeFiles/jir_seg.dir/all] Error 2
make: *** [all] Error 2

UIC 添加到 ui_*.h 的标题是 '#include "JIRSEGSubClassQGV.hpp"',我尝试了一些方法来解决,但没有一个不起作用,感谢任何帮助。

【问题讨论】:

我不确定,但可能是explicit reference on path of include files 帮助。 非常感谢,我也试过这个 include_directories($CMAKE_CURRENT_SOURCE_DIR) set(HEADER_FILES $CMAKE_CURRENT_SOURCE_DIR/include/jir_seg/JIRSEGSubClassQGV.hpp),但它也不起作用,我'不确定,但我认为我做错了,我找不到它! 您是否同时尝试include_directories($CMAKE_CURRENT_SOURCE_DIR)include_directories($CMAKE_CURRENT_SOURCE_DIR/include)include_directories($CMAKE_CURRENT_SOURCE_DIR/include/jir_seg) - 所有三个指令? 并尝试在PROJECT_SOURCE_DIR 上替换CMAKE_CURRENT_SOURCE_DIR。通常我只使用PROJECT_SOURCE_DIR 非常感谢,花了我大约5个小时的时间,第一个分层指令解决了我的问题,请把它写成答案:) 【参考方案1】:

CMake 不知道目录的用途,因此如果将头文件放在子目录include 中,则必须使用iclude_directories 指令通知CMake

include_directories($CMAKE_CURRENT_SOURCE_DIR) 包含目录($CMAKE_CURRENT_SOURCE_DIR/include) include_directories($CMAKE_CURRENT_SOURCE_DIR/include/jir_seg)

【讨论】:

以上是关于使用 cmake 和提升的小部件编译 qt gui 时出错的主要内容,如果未能解决你的问题,请参考以下文章

如何关闭在提升的小部件中单击的按钮上的 qt 小部件 ui?

QT - 在不阻止他的 GUI 的情况下显示小部件

如何为 Qt GUI 小部件使用图像

Qt:访问在构造函数之外使用 GUI 小部件的类的函数

cmake编译Qt工程

在 Qt 中的小部件内部绘画