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