fastdda+ubuntu20.04+vscode实现(实测最简单安装教程)

fastdda+ubuntu20.04+vscode实现及各种问题处理

ubuntu镜像安装包及vm+密钥,私信我给你网盘资源(单纯懒得这会挂连接)

fastdds虚拟机安装

Dds安装(ubuntu)

打开终端

1.更新工具(建议使用要不可能会出现一堆问腿)

sudo apt update

sudo apt upgrade

2.安装构建Fast DDS所需的依赖项

sudo apt install -y cmake git gcc g++ libssl-dev

3.获取Fast DDS源代码

3.1克隆Fast DDS的GitHub仓库:

git clone --recursive https://github.com/eProsima/Fast-DDS.git

3.2进入Fast DDS目录:

cd Fast-DDS

4.构建和安装Fast DDS

4.1创建一个构建目录并进入:

mkdir build && cd build

4.2配置CMake项目:

cmake -DTHIRDPARTY=ON -DBUILD_SHARED_LIBS=ON ..

4.3开始构建过程(这个会有一点点久,建议使用下面的管理员权限安装):

cmake --build . --target install

如果报错使用

sudo cmake --build . --target install

5.验证是否成功

检查Fast DDS库文件是否已经正确安装在系统的库目录中,如/usr/local/lib。使用ls命令来查看这些文件:

ls /usr/local/lib | grep fastrtps

成功安装后显示类似

命令行$ ls /usr/local/lib | grep fastrtps
libfastrtps.so
libfastrtps.so.2.14
libfastrtps.so.2.14.0

6.安装vscode,直接软件商店直接安装

疑难问题报错

1.cmake -DTHIRDPARTY=ON -DBUILD_SHARED_LIBS=ON ..   报错

CMake无法找到foonathan_memory库的配置文件。foonathan_memory是Fast DDS依赖的一个库通常,foonathan_memory库可以和Fast DDS一起作为第三方库自动获取和编译。如果失败,需要手动获取和安装这个库:

1)克隆foonathan_memory库的仓库:

git clone https://github.com/foonathan/memory.git

2)进入克隆的目录:

cd memory

3)创建一个构建目录并进入:

mkdir build && cd build

4)运行CMake配置:

cmake ..

5)构建并安装库:

cmake --build . --target install

这里如果报错改用管理员权限

sudo cmake --build . --target install

2.无法连接git 即无法进行克隆

重启虚拟机,运行

sudo apt update

sudo apt upgrade

如果还是不行建议重启vm(重复上述)

vscode编写代码运行

在Ubuntu环境下,使用Fast DDS在VSCode中创建一个简单的DDS发布者(Publisher)和订阅者(Subscriber)实现dds通信。

1.配置VSCode环境

确保VSCode已经安装。

安装C/C++扩展

2.创建和配置项目

在VSCode中,创建一个新的工作空间并为项目创建一个文件夹,比如DDSExample。

在DDSExample文件夹中,创建两个源文件:Publisher.cpp和Subscriber.cpp。

创建CMakeLists.txt文件来定义项目、指定Fast DDS库依赖。

下面是相关代码

//简单的发布者逻辑模板

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>

#include <fastdds/dds/publisher/DataWriter.hpp>

#include <fastdds/dds/publisher/Publisher.hpp>

#include <fastdds/dds/topic/Topic.hpp>

#include <fastrtps/attributes/ParticipantAttributes.h>

#include <fastrtps/attributes/PublisherAttributes.h>

#include <fastrtps/participant/Participant.h>

#include <fastrtps/publisher/Publisher.h>

using namespace eprosima::fastdds::dds;

int main() {

    DomainParticipant* participant = DomainParticipantFactory::get_instance()->create_participant(0);

    // 创建发布者逻辑...

    // 注意:实际代码中你需要添加更多的逻辑来定义数据类型、创建主题、创建发布者等。

    // 清理资源

    DomainParticipantFactory::get_instance()->delete_participant(participant);

  

    return 0;

}
//简单的订阅者逻辑模板

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>

#include <fastdds/dds/subscriber/DataReader.hpp>

#include <fastdds/dds/subscriber/Subscriber.hpp>

#include <fastdds/dds/topic/Topic.hpp>

#include <fastrtps/attributes/ParticipantAttributes.h>

#include <fastrtps/attributes/SubscriberAttributes.h>

#include <fastrtps/participant/Participant.h>

#include <fastrtps/subscriber/Subscriber.h>

using namespace eprosima::fastdds::dds;

int main() {

    DomainParticipant* participant = DomainParticipantFactory::get_instance()->create_participant(0);

    // 创建订阅者逻辑...

    // 注意:实际代码中你需要添加更多的逻辑来定义数据类型、创建主题、创建订阅者等。

    // 清理资源

    DomainParticipantFactory::get_instance()->delete_participant(participant);

    return 0;

}
//CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(DDSExample)

find_package(fastrtps REQUIRED)

add_executable(Publisher Publisher.cpp)

target_link_libraries(Publisher fastrtps)

add_executable(Subscriber Subscriber.cpp)

target_link_libraries(Subscriber fastrtps)

3.在VSCode的终端中,运行以下命令来构建项目

mkdir build && cd build

cmake ..

make

4.在build目录中,运行生成的发布者和订阅者可执行文件来测试(make后会在build中生成相关文件,要有发布者与订阅者的可执行文件,主要是运行这俩个)

./Publisher

./Subscriber

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

01_

感谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值