hello-world-fastdds

本章我们运行第一个fastdds示例——hello-world-fastdds。步骤如下:

    1. 创建消息.IDL文件
    1. 使用fastddsgen生成消息对应的源文件
    1. 编译、运行示例

创建消息文件

struct hello_world
{
    string name;
    int counter;
};

生成消息对应的源文件

fastddsgen会在命令运行的文件夹下生成文件。

export PATH=${FAST_DDS_ROOT}/Fast-DDS-Gen/scripts:$PATH
fastddsgen -example CMake hello_fastdds.IDL

编译运行

修改编译脚本

修改CMake文件,增加相关库的查找路径

# 这三个是必要的,因为
set(fastcdr_DIR $ENV{FAST_DDS_ROOT}/install/lib/cmake/fastcdr)
set(fastrtps_DIR $ENV{FAST_DDS_ROOT}/install/share/fastrtps/cmake)
set(foonathan_memory_DIR $ENV{FAST_DDS_ROOT}/install/lib/foonathan_memory/cmake)

编写编译脚本

#!/bin/bash


export FAST_DDS_ROOT=~/work/code/third_party/Fast-DDS-GIT
export LD_LIBRARY_PATH=${FAST_DDS_ROOT}/install/lib:$LD_LIBRARY_PATH

rm build -rf

DIR = $PWD

mkdir build  && cd build &&  cmake ../ &&  make -j4

cd $DIR

运行程序

运行程序前,需要指定依赖库的查找路径,命令和运行结果分别如下:

运行发布者

export FAST_DDS_ROOT=~/work/code/third_party/Fast-DDS-GIT
export LD_LIBRARY_PATH=${FAST_DDS_ROOT}/install/lib:$LD_LIBRARY_PATH

./hello_fastdds publisher
# Starting 
# hello_world DataWriter created.
# hello_world DataWriter waiting for DataReaders.
# DataWriter matched.

运行消费者

export FAST_DDS_ROOT=~/work/code/third_party/Fast-DDS-GIT
export LD_LIBRARY_PATH=${FAST_DDS_ROOT}/install/lib:$LD_LIBRARY_PATH

./hello_fastdds subscriber
# Starting
# Waiting for Data, press Enter to stop the DataReader.
# Subscriber matched.
# Sample received, count=1