【ROS/DDS】FastDDS: 源码构建及问题分析(二)

1. FastDDS 各项依赖版本

官方文档地址:Fast DDS 官方文档
支持的 Ubuntu 系统版本:Ubuntu 20.04, Ubuntu 22.04

1.1 Ubuntu20.04 + ROS2-Foxy版本

foonathan_memory_vendor 版本:v1.2.2
Fast-CDR 版本:v1.0.13
Fast-DDS 版本:v2.14.3
FastDDS-gen 版本:v2.5.2

2. 源码编译安装 FastDDS

2.1 前置依赖条件
• CMake, g++, pip3, wget and git

sudo apt update
sudo apt install cmake g++ python3-pip wget git

○ Asio ,TinyXML2libraries, OpenSSL

sudo apt install libasio-dev libtinyxml2-dev libssl-dev libp11-dev

○ Gtest

git clone https://github.com/google/googletest.git
cd  googletest/
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
sudo cmake --build . --target install -j8

2.2 编译

2.2.1 创建工作目录

Bash

mkdir ~/Fast-DDS

2.2.2 安装所有依赖项

2.2.2.1 Foonathan memory
cd ~/Fast-DDS
git clone https://github.com/eProsima/foonathan_memory_vendor.git
mkdir foonathan_memory_vendor/build
cd foonathan_memory_vendor/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/
sudo cmake --build . --target install
2.2.2.2 Fast CDR
cd ~/Fast-DDS
git clone https://github.com/eProsima/Fast-CDR.git
mkdir Fast-CDR/build
cd Fast-CDR/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/ -DBUILD_SHARED_LIBS=ON
sudo cmake --build . --target install
2.2.2.3 Fast DDS
cd ~/Fast-DDS
git clone https://github.com/eProsima/Fast-DDS.git
git checkout 2.14.3
mkdir Fast-DDS/build
cd Fast-DDS/build
cmake ..  -DCMAKE_INSTALL_PREFIX=/usr/local/ -DBUILD_SHARED_LIBS=ON
sudo cmake --build . -j16 --target install
2.2.2.4 编译Fast DDS Gen

Fast-DDS-Gen 是一个 Java 工具,用于生成与 eProsima Fast DDS 库兼容的 TypeSupport 代码。该工具通过解析 IDL(接口定义语言)文件,自动生成数据类型的源代码,从而简化了 Fast DDS 应用程序的开发过程

mkdir -p ~/Fast-DDS/src
cd ~/Fast-DDS/src
git clone --recursive https://github.com/eProsima/Fast-DDS-Gen.git fastddsgen
cd fastddsgen
./gradlew assemble
注意:库路径 --环境变量(如果指定安装到~/Fast-DDS/install/中才执行)
export LD_LIBRARY_PATH=~/Fast-DDS/install/lib
#或者放到.bashrc
echo 'export LD_LIBRARY_PATH=~/Fast-DDS/install/lib' >>~/.bashrc

2.3 简单运行example :HelloWorldExample(测试FastDDS通讯是否有异常)

• 开一个终端

#export LD_LIBRARY_PATH=~/Fast-DDS/install/lib 视情况添加
cd ~/Fast-DDS/install/examples/cpp/dds/HelloWorldExample/bin
./DDSHelloWorldExample publisher 100

• 新开一个终端

#export LD_LIBRARY_PATH=~/Fast-DDS/install/lib 视情况添加
cd ~/Fast-DDS/install/examples/cpp/dds/HelloWorldExample/bin
./DDSHelloWorldExample subscriber

3. 如何使用FastDDS-Gen 生成代码?

FastDDS-Gen 是一个 Java 工具,用于生成与 eProsima Fast DDS 库兼容的 TypeSupport 代码。通过解析 IDL(接口定义语言)文件,FastDDS-Gen 自动生成数据类型的源代码,简化了 Fast DDS 应用程序的开发过程。

3.1 创建 IDL 文件

首先,创建一个 IDL 文件,描述你希望在 Fast DDS 中使用的数据类型。
例如,创建一个简单的 Test.idl 文件,内容如下:

struct TestMessage{
    string message;
    long id;
};

3.2 使用 FastDDS-Gen 生成代码

/home/glr/Fast-DDS/src/fastddsgen/scripts/fastddsgen -example CMake Test.idl

生成的文件将包括 Test.hpp、 TestPubSubTypes.hpp等。(如下图所示)
在这里插入图片描述

3.3 编译和运行示例应用程序

Fast-DDS-Gen 还可以生成示例应用程序:

mkdir build
cd build
cmake ..
make -j10

运行生成的示例应用程序:

./Test publisher
./Test subscriber

4. 报错及问题分析

4.1 正常使用Fast—DDS报错(未找到fastcdr 可能指定安装到~/Fast-DDS/install/)

在这里插入图片描述

设置 CMAKE_PREFIX_PATH 或 fastcdr_DIR

为了让 CMake 找到 fastcdr 的配置文件,您可以设置 CMAKE_PREFIX_PATH 或 fastcdr_DIR 环境变量。这里推荐使用 fastcdr_DIR,因为它更具体:
设置 fastcdr_DIR

export fastcdr_DIR=~/Fast-DDS/install/lib/cmake/fastcdr
然后重新运行 CMake:
Bash
cd ~/Code/dds_tutorial/examples/01-hellofishros/build
cmake ..

还有一种方法

如果您选择使用 CMAKE_PREFIX_PATH 来帮助 CMake 找到 fastcdr 库,可以通过设置该环境变量来指定库的安装前缀路径。CMAKE_PREFIX_PATH 是一个包含多个路径的列表,CMake 会在这些路径中查找所需的包配置文件和模块。

设置 CMAKE_PREFIX_PATH

您可以将 fastcdr 的安装目录添加到 CMAKE_PREFIX_PATH 中,这样 CMake 就会在这个路径下寻找 fastcdrConfig.cmake 或 fastcdr-config.cmake 文件。以下是具体步骤:

临时设置(仅在当前终端会话中有效)

在命令行中临时设置 CMAKE_PREFIX_PATH 环境变量:

export CMAKE_PREFIX_PATH=~/Fast-DDS/install:$CMAKE_PREFIX_PATH

然后重新运行 CMake:

cd ~/Code/dds_tutorial/examples/01-hellofishros/build

cmake …

4.2 安装Fastdds-gen报无java错误

glr@glr-desktop:~/Fast-DDS/src/fastddsgen$ ./gradlew assemble

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

在 Ubuntu 上安装 OpenJDK

sudo apt update
sudo apt install -y openjdk-11-jdk  # 或者 openjdk-8-jdk, 根据需求选择版本

4.3 在Ubuntu20.04编译FastDDS可能遇到的问题

问题:CMake 3.20 or higher is required. You are running version 3.16.3(Cmake版本不支持)

• 在CMake官网下载对应版本的安装包进行安装
• 打开cmake下载的官网:https://cmake.org/files/

sudo wget https://cmake.org/files/v3.23/cmake-3.23.0.tar.gz
 # 解压
sudo tar -zxvf cmake-3.23.0.tar.gz
 # cmake-3.23.0文件夹
cd cmake-3.23.0
 # 安装
 sudo ./configure

4.4 版本导致问题

版本问题1:编译Fast-DDS源码,报错找不到foonathan_memoryConfig.cmake错误

在这里插入图片描述

#由于版本的问题导致cmake文件无法获取,建议选择checkout 版本 v1.0.0

在这里插入图片描述

版本问题2:Build程序报错 does not name a type

在这里插入图片描述

版本不匹配问题,建议切换fast-dds版本与fast-cdr 等依赖对应
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monster_H777

我直说吧:你的奖励我的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值