如何在ROS中链接Qt 5.9?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在ROS中链接Qt 5.9?相关的知识,希望对你有一定的参考价值。

以下CMakeList会产生以下列出的错误:

cmake_minimum_required(VERSION 2.8.12)

set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++11")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

project(draw_on_images)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
)

find_package(OpenCV 2.4.8 REQUIRED)
find_package(Qt5 COMPONENTS Core Qml Quick REQUIRED)


qt5_add_resources(RESOURCES qml.qrc)

catkin_package(
  CATKIN_DEPENDS roscpp rospy std_msgs
)

include_directories(
  $catkin_INCLUDE_DIRS
)

add_executable($PROJECT_NAME $RESOURCES src/main.cpp src/draw_on_qimage.cpp src/provide_qpixmap_to_qml.cpp )

qt5_use_modules($PROJECT_NAME Core Qml Quick)

target_link_libraries($PROJECT_NAME Qt5::Core Qt5::Qml Qt5::Quick $OpenCV_LIBS)

我运行它的方式:

catkin_make -DCMAKE_PREFIX_PATH="/opt/Qt5.9.1/5.9.1/gcc_64/lib/" -DWITH_QT:BOOL=ON

是的我已经采购了它。

错误:

Base path: /home/.../ros_workspace_test
Source space: /home/.../ros_workspace_test/src
Build space: /home/.../ros_workspace_test/build
Devel space: /home/.../ros_workspace_test/devel
Install space: /home/.../ros_workspace_test/install
####
#### Running command: "cmake /home/.../ros_workspace_test/src -DCMAKE_PREFIX_PATH=/opt/Qt5.9.1/5.9.1/gcc_64/lib/ -DWITH_QT:BOOL=ON -DCATKIN_DEVEL_PREFIX=/home/.../ros_workspace_test/devel -DCMAKE_INSTALL_PREFIX=/home/.../ros_workspace_test/install -G Unix Makefiles" in "/home/.../ros_workspace_test/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/.../ros_workspace_test/devel
-- Using CMAKE_PREFIX_PATH: /opt/Qt5.9.1/5.9.1/gcc_64/lib/
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/.../ros_workspace_test/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.6.19
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - draw_on_images
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'draw_on_images'
-- ==> add_subdirectory(draw_on_images)
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    WITH_QT


-- Build files have been written to: /home/.../ros_workspace_test/build
####
#### Running command: "make -j4 -l4" in "/home/.../ros_workspace_test/build"
####
[ 14%] Automoc for target draw_on_images
[ 14%] Built target draw_on_images_automoc
Linking CXX executable /home/.../ros_workspace_test/devel/lib/draw_on_images/draw_on_images
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `int qmlRegisterType<Draw_on_qimage>(char const*, int, int, char const*)':
main.cpp:(.text._Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_[_Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_]+0x38): undefined reference to `Draw_on_qimage::staticMetaObject'
main.cpp:(.text._Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_[_Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_]+0x1b6): undefined reference to `Draw_on_qimage::staticMetaObject'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `Draw_on_qimage::~Draw_on_qimage()':
main.cpp:(.text._ZN14Draw_on_qimageD2Ev[_ZN14Draw_on_qimageD5Ev]+0x13): undefined reference to `vtable for Draw_on_qimage'
main.cpp:(.text._ZN14Draw_on_qimageD2Ev[_ZN14Draw_on_qimageD5Ev]+0x25): undefined reference to `vtable for Draw_on_qimage'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `QMetaTypeIdQObject<Draw_on_qimage*, true>::qt_metatype_id()':
main.cpp:(.text._ZN18QMetaTypeIdQObjectIP14Draw_on_qimageLb1EE14qt_metatype_idEv[_ZN18QMetaTypeIdQObjectIP14Draw_on_qimageLb1EE14qt_metatype_idEv]+0x2c): undefined reference to `Draw_on_qimage::staticMetaObject'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `QtPrivate::MetaObjectForType<Draw_on_qimage*, true>::value()':
main.cpp:(.text._ZN9QtPrivate17MetaObjectForTypeIP14Draw_on_qimageLb1EE5valueEv[_ZN9QtPrivate17MetaObjectForTypeIP14Draw_on_qimageLb1EE5valueEv]+0x7): undefined reference to `Draw_on_qimage::staticMetaObject'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x10): undefined reference to `Draw_on_qimage::metaObject() const'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x18): undefined reference to `Draw_on_qimage::qt_metacast(char const*)'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x20): undefined reference to `Draw_on_qimage::qt_metacall(QMetaObject::Call, int, void**)'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTIN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTIN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x10): undefined reference to `typeinfo for Draw_on_qimage'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `Provide_QPixmap_to_QML::~Provide_QPixmap_to_QML()':
main.cpp:(.text._ZN22Provide_QPixmap_to_QMLD2Ev[_ZN22Provide_QPixmap_to_QMLD5Ev]+0x13): undefined reference to `vtable for Provide_QPixmap_to_QML'
main.cpp:(.text._ZN22Provide_QPixmap_to_QMLD2Ev[_ZN22Provide_QPixmap_to_QMLD5Ev]+0x25): undefined reference to `vtable for Provide_QPixmap_to_QML'
CMakeFiles/draw_on_images.dir/src/draw_on_qimage.cpp.o: In function `Draw_on_qimage::Draw_on_qimage(QQuickPaintedItem*)':
draw_on_qimage.cpp:(.text+0x4b): undefined reference to `vtable for Draw_on_qimage'
draw_on_qimage.cpp:(.text+0x5d): undefined reference to `vtable for Draw_on_qimage'
CMakeFiles/draw_on_images.dir/src/provide_qpixmap_to_qml.cpp.o: In function `Provide_QPixmap_to_QML::Provide_QPixmap_to_QML()':
provide_qpixmap_to_qml.cpp:(.text+0x52): undefined reference to `vtable for Provide_QPixmap_to_QML'
provide_qpixmap_to_qml.cpp:(.text+0x64): undefined reference to `vtable for Provide_QPixmap_to_QML'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/.../ros_workspace_test/devel/lib/draw_on_images/draw_on_images] Error 1
make[1]: *** [draw_on_images/CMakeFiles/draw_on_images.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

如何在ROS中链接Qt 5.9?

我正在使用Ubuntu 14。

答案

解决方法是将头文件添加到CMakelists.txt中的add_executable

add_executable($PROJECT_NAME $RESOURCES
                               src/main.cpp 
                               include/draw_on_images/draw_on_qimage.h src/draw_on_qimage.cpp
                               include/draw_on_images/provide_qpixmap_to_qml.h src/provide_qpixmap_to_qml.cpp)
另一答案

我的猜测是CMake的AUTOMOC在这种情况下失败了。请参阅此处的文档https://cmake.org/cmake/help/v3.0/prop_tgt/AUTOMOC.html它断言,类Draw_on_qimage的头文件类似地命名为源文件,可由CMake定位。

您可以通过在此标头上为您的QObject派生类手动运行moc来避免AUTOMOC

 QT5_WRAP_CPP(MOC_SOURCES <path of header file of your class Draw_on_qimage>)

 add_executable($PROJECT_NAME $RESOURCES src/main.cpp src/draw_on_qimage.cpp $MOC_SOURCES src/provide_qpixmap_to_qml.cpp 

以上是关于如何在ROS中链接Qt 5.9?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Qt插件在Qt中进行ROS开发

ROS订阅的topic如何通过tcp传送给QT并显示到界面上

如何更加便捷的调试ROS(Robot Operating System)

Qt 5.9 安装过程报错现象及解决方案

Qt 5.9 for Android 实现沉浸式状态栏

如何将基于 qt 的应用程序(在 Mac 上开发)部署到 Windows?