【C++】源码编译 Qt5.15.3|Ubuntu22.04 下 ROS 开发环境搭建

🔥大奇个人主页 :https://blog.csdn.net/m0_75192474?type=blog

本文所属专栏:https://blog.csdn.net/m0_75192474/category_13131150.html

下载安装包和依赖

复制代码
wget https://download.qt.io/archive/qt/5.15/5.15.3/single/qt-everywhere-opensource-src-5.15.3.tar.xz

tar -xf qt-everywhere-opensource-src-5.15.3.tar.xz

解压好之后大概3.3G,确保有充足的内存空间

配置安装脚本并编译

安装依赖

复制代码
sudo apt update
sudo apt install build-essential libssl-dev libxcb-xinerama0-dev libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxkbcommon-dev libxkbcommon-x11-dev

sudo apt install -y \
libxcb1-dev \
libxcb-xinerama0-dev \
libxcb-randr0-dev \
libxcb-shape0-dev \
libxcb-sync-dev \
libxcb-xfixes0-dev \
libxcb-xkb-dev \
libxcb-icccm4-dev \
libxcb-image0-dev \
libxcb-keysyms1-dev \
libxcb-render-util0-dev \
libxkbcommon-x11-dev \
libx11-xcb-dev \
libgl1-mesa-dev

依赖安装成功后进入包目录

复制代码
cd qt-everywhere-opensource-src-5.15.3

现在编译会出现问题,因此找到路径**qtbase/src/corelib/text/qbytearraymatcher.h文件,并添加头文件#include <limits.h>**

接下执行

复制代码
mkdir build
cd build

执行配置脚本

复制代码
../configure -prefix /opt/qt5.15.3 -opensource -confirm-license -release -shared -nomake examples -nomake tests -xcb -xkbcommon -c++std c++17 -skip qtdeclarative
参数 作用
../configure 调用 Qt 源码的配置脚本(必须在新建的编译目录中执行,不能在源码根目录直接运行)
-prefix /opt/qt5.15.3 编译完成后 Qt 的安装路径,所有库、头文件、工具都会安装到这里
-opensource 使用 Qt 开源版本
-confirm-license 自动确认接受开源协议
-release 编译发行版(无调试信息,体积小、运行快)
-shared 编译动态库(.so 文件)
-nomake examples 不编译示例代码(大幅缩短编译时间,节省空间)
-nomake tests 不编译测试用例(同上,加速编译)
-xcb 启用 Linux X11 窗口系统支持(桌面程序必须开启)
-xkbcommon 启用键盘输入支持(Linux 输入法 / 键盘依赖)
-c++std c++17 指定使用 C++17 标准编译
-skip qtdeclarative 跳过 Qt QML/Quick 模块(因为只开发传统 Qt Widgets 桌面程序,不需要 QML,跳过可节省大量时间)

配置成功会在终端输出,说明配置脚本成功

复制代码
Qt is now configured for building. Just run 'gmake'.
Once everything is built, you must run 'gmake install'.
Qt will be installed into '/opt/Qt5.15.3'.

接下来执行编译

复制代码
make -j$(nproc)
bash 复制代码
sudo make install

以上这两条命令执行后不报错即可

配置环境变量

bash 复制代码
sudo gedit ~/.bashrc

如果没有安装gedit可以执行

bash 复制代码
sudo apt insatll gedit

复制以下内容到末尾

bash 复制代码
export QTDIR=/opt/qt5.15.3
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=$QTDIR/plugins:$QT_PLUGIN_PAT

保存后执行

bash 复制代码
source ~/.bashrc
bash 复制代码
qmake -v

输出以下内容说明成功

安装qtcreator11.0

安装地址https://download.qt.io/official_releases/qtcreator/11.0/11.0.0/qt-creator-opensource-linux-x86_64-11.0.0.run

点击可直接下载

安装的时候如不想注册账号,断网即可

qtcreator配置编译套件

点击下载好的.run文件,打开Qt Creater,按照图示打开

选择我们源码编译的那个目录里的qmake

安装qt插件(ros)

Github地址https://github.com/ros-industrial/ros_qtc_plugin/releases/download/11.0/ROSProjectManager-11.0-Linux-x86_64.zip

将插件导入qt中

创建qt的ros项目

参考【机器人】ROS结合Qt开发上位机软件工作空间配置-CSDN博客

创建一个工作空间,需包含src目录,然后打开qt creator ,选择创建新项目,选择其他项目,ros

  • 右键src添加qt设计师界面类,选择widgets
  • 然后随便搞一个名字,这里叫ros2_Gui
  • 在右键src添加c++source文件,名字为main函数
  • 然后再配置cmakelist
cpp 复制代码
cmake_minimum_required(VERSION 3.8)
project(looraysBot_rviz)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS
    Core Widgets)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
include_directories(
  include
  ${Qt5Widgets_INCLUDE_DIRS}
  ${rclcpp_INCLUDE_DIRS}
)
add_executable(looraysBot_Gui
  src/main.cpp
  src/ros2gui.cpp
  src/ros2gui.h
  src/ros2gui.ui
)

ament_target_dependencies(looraysBot_Gui rclcpp)

target_link_libraries(looraysBot_Gui Qt5::Widgets)

install(TARGETS looraysBot_Gui
    DESTINATION lib/${PROJECT_NAME})

# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

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()

main函数

cpp 复制代码
#include <QApplication>
#include <QMainWindow>
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/laser_scan.hpp>
#include "ros2gui.h"
int main(int argc, char *argv[])
{

    rclcpp::init(argc, argv);
    QApplication a(argc, argv);
    ros2Gui w;
    w.show();
    int res = a.exec();

    rclcpp::shutdown();
    return res;
}

编译程序

左下角选择编译套件,为源码编译的套件,然后直接点锤子,编译

选择你ros包生成的那个节点可执行文件,就是cmakelist里面的add_exautle,在 install目录

再点击运行即可

效果(实际开发好的上位机)

相关推荐
数聚天成DeepSData4 小时前
企业知识库 RAG 数据准备与文档清洗:Dify、RAGFlow、扣子选型指南
开发语言·人工智能·机器学习·自然语言处理·sentinel·cocos2d
我是唐青枫4 小时前
Java SLF4J 实战指南:从日志门面到 Logback、MDC 和链路追踪
java·开发语言·logback
aramae4 小时前
C++11:现代C++的里程碑
c语言·开发语言·c++·windows·git·后端
weixin_446729164 小时前
java实现发送邮件
java·开发语言
米尔的可达鸭5 小时前
深入操作系统 Socket 底层:EPOLLOUT 可写事件管理 + 非阻塞异步
开发语言·网络·数据结构·经验分享·websocket·网络协议
前端进阶之旅5 小时前
Next.js 16.3 新特性全解析 AI 驱动开发与 Instant Navigations 实战指南
开发语言·javascript·人工智能
小小编程路5 小时前
Java super 关键字详解:用法、场景与常见误区
java·开发语言
薛定猫AI5 小时前
【技术干货】大模型检查点可靠性评测:基于 Python 构建幻觉与指令遵循测试
开发语言·python
文祐5 小时前
C语言利用数组计算矩阵乘法
c语言·开发语言·矩阵
啦啦啦啦啦zzzz5 小时前
c++web服务器框架Oat++的API应用
服务器·c++·oat++