FastDDS 食用说明
1. 下载安装
1. 安装 FastDDS
2. 安装 FastDDS-Gen
首先安装 JDK 和 Gradle
Gradle 可以前往官网下载官网 Release 页面
我这边下载的是
complete
版本,7.5.1
的下载完后解压,然后把目录下的
bin
文件夹加入环境变量就可以使用了,可以进cmd
运行gradle -v
查看是否配置成功
git 下载 FastDDS-Gen 源码
git clone --recursive https://github.com/eProsima/Fast-DDS-Gen.git
进入目录后输入
gradle assemble
等待安装完成即可。最后将
FastDDS-Gen
目录下的scripts
路径加入环境变量即可
2. 在 QT 中使用
数据类型
新建一个 QT 项目,使用
cmake
和MSVC
在
CMakeLists.txt
中加入如下代码:
# cmake的最低版本
cmake_minimum_required(VERSION 3.12.4)
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
cmake_policy(SET CMP0048 NEW)
endif()
# 项目名,QT已经自动配置了,这里可以不需要
project(DDSHelloWorld)
# 这里添加你的fastdds安装目录即可,要加引号
set(CMAKE_PREFIX_PATH "D:/Code/eProsima/fastrtps 2.8.0")
# Find requirements
if(NOT fastcdr_FOUND)
find_package(fastcdr REQUIRED)
endif()
if(NOT fastrtps_FOUND)
find_package(fastrtps REQUIRED)
endif()
# Set C++11
include(CheckCXXCompilerFlag)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG OR
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)
if(SUPPORTS_CXX11)
add_compile_options(-std=c++11)
else()
message(FATAL_ERROR "Compiler doesn't support C++11")
endif()
endif()
# 引入Include路径
include_directories("D:/Code/eProsima/fastrtps 2.8.0/include")
Qt6.4.0
中默认使用的是C++17
,因为FastDDS
官方文档写的是C++11
,因此可以改成C++11
。当然你要用 17 应该也行?之后在目录下新建
HelloWorld.idl
文件,并写入:
struct HelloWorld
{
unsigned long index;
string message;
};
cmd 进入项目目录,输入
fastddsgen HelloWorld.idl
等待文件生成如果提示没有
cl.exe
需要把msvc
的cl.exe
所在路径加入环境变量(可以用Everything
进行查找)生成的文件结构如下:
- 项目目录
- HelloWorld.cxx: HelloWorld类型定义。
- HelloWorld.h: HelloWorld.cxx的头文件。
- HelloWorldPubSubTypes.cxx: HelloWorld类型的序列化和反序列化代码。
- HelloWorldPubSubTypes.h: HelloWorldPubSubTypes.cxx的头文件。
2. 发布者
首先下载 HelloWorld 文件: HelloWorldPublisher.cpp
有关代码解释可以阅读: 官方文档 ,写的很详细
为了便于阅读,我将其拆分为头文件和 cpp 文件
Last modified: 16 十一月 2023