ROS2 Foxy + Qt5 on Linux Platform
按上面两个文章配置后的目录结构
├── build
├── CMakeLists.txt
├── include
│ ├── mainwindow.h
│ └── rclcomm.h
├── package.xml
├── resource
├── src
│ ├── main.cpp
│ ├── mainwindow.cpp
│ └── rclcomm.cpp
└── ui
└── mainwindow.ui
rclcomm.cpp和rclcomm.h是我的测试文件
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(ros2_qt_demo LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# !!! 第一个要注释掉
# set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
# ROS
# !!!ros需要的功能在这里导入
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
# QT
find_package(Qt5 COMPONENTS Widgets REQUIRED)
# find_package(<dependency> REQUIRED)
# 项目目录下的目标配置文件
file(GLOB PRO_SOURCE_DIR RELATIVE ${CMAKE_SOURCE_DIR} FOLLOW_SYMLINKS src/*.cpp)
file(GLOB PRO_UI_DIR RELATIVE ${CMAKE_SOURCE_DIR} FOLLOW_SYMLINKS ui/*.ui)
file(GLOB PRO_INCLUDE_DIR RELATIVE ${CMAKE_SOURCE_DIR} FOLLOW_SYMLINKS include/*.h *.hpp)
file(GLOB PRO_RESOURCE_DIR RELATIVE ${CMAKE_SOURCE_DIR} FOLLOW_SYMLINKS resource/*.qrc)
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
#搜索头文件的位置
include_directories(
include
${rclcpp_INCLUDE_DIRS}# !!! 编译找不到rclcpp.hpp,要加上这个
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
# include_directories(${rclcpp_INCLUDE_DIRS})
# (输出, 输入)
qt5_wrap_ui(QT_UI_HPP ${PRO_UI_DIR})
qt5_wrap_cpp(QT_MOC_HPP ${PRO_INCLUDE_DIR})
qt5_add_resources(QT_RESOUCE_HPP ${PRO_RESOURCE_DIR})
# 导入可执行的目标文件
add_executable(${PROJECT_NAME}
${PRO_SOURCE_DIR}
${PRO_INCLUDE_DIR}
${PRO_UI_DIR}
${PRO_RESOURCE_DIR}
${QT_UI_HPP} # UI的文件应该不需要添加在这里
${QT_MOC_HPP}
${QT_RESOUCE_HPP}
)
# !!! 编译找不到rclcpp.hpp,这里要加上${rclcpp_LIBRARIES}
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets ${rclcpp_LIBRARIES})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
这个是我配置后的qt_ros_demo