ROS下安装vscode并配置ROS编译环境

本文详细介绍了如何在Docker环境下安装和配置ROS,并使用VSCode进行开发。首先,通过安装deb包和解决依赖问题来安装VSCode。接着,创建工作区,使用catkin_make进行编译。然后,配置VSCode的任务和插件,包括修改tasks.json以适应编译需求。此外,还解决了VSCode中文乱码的问题,通过更换软件源来修复字体显示。整个过程涵盖了从环境准备到实际开发的完整步骤。
摘要由CSDN通过智能技术生成

(我这是基于docker下的ros当然你不用docker用实体机或者虚拟机也行)
效果如下
参考的大佬视频
——————————————————————
我的docker下的ros环境安装链接
——————————————————————
在这里插入图片描述

一.安装

1.安装与打开

0积分下载vscode安装包
安装deb安装包的命令,注意下面的 ./

sudo apt install ./code_1.59.1-1629375198_amd64.deb

如下报错说明缺失依赖项

Note, selecting 'code' instead of './code_1.59.1-1629375198_amd64.deb'
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 code : Depends: libnss3 (>= 2:3.26) but it is not installable
E: Unable to correct problems, you have held broken packages.

sudo apt-get install libnss3

然后启动vscode

code ./  

如果报错

To no longer see this prompt, start Visual Studio Code with the environment variable DONT_PROMPT_WSL_INSTALL defined. You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the `--user-data-dir` argument.

试试下面的命令

sudo code /directory-to-open --user-data-dir='.' --no-sandbox

在这里插入图片描述
为了方便后续启动,这里可以编写一个 v.sh 脚本文件用于启动

gedit v.sh

内部如下
在这里插入图片描述

echo"begin start vscode....."
code /directory-to-open --user-data-dir='.' --no-sandbox

然后加上启动权限

chmod o+x v.sh

或以下这样的可执行权限

sudo chmod -R 777 v.sh

让通过./ 启动

./v.sh

sudo ./v.sh

在这里插入图片描述
后续配置参考(不过中文可能会乱码)

2.安装相关插件

如下,中文的话,搜索 Chinese 第一个就是,不过如果你系统如果中文有问题先不用装这个,中文乱码处理方案见本文最后
在这里插入图片描述

二.使用

1.手动建立工作区

例如我这建立的是一个叫做 cs_ws的工作区(必须得有 src)

mkdir -p cs_ws/src
cd cs_ws
catkin_make

在这里插入图片描述

2.启动vscode打开之前建立的工作区

./v.sh 

在这里插入图片描述

在这里插入图片描述

3.准备编译的命令与参数

按下快捷键

Ctrl+Shift+B
打开catkin_make 的配置选项
在这里插入图片描述
默认是这样的

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "catkin_make_isolated",
			"args": [
				"--directory",
				"/root/cs_ws",
				"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
			],
			"problemMatcher": [
				"$catkin-gcc"
			],
			"group": "build",
			"label": "catkin_make_isolated: build"
		}
	]
}

修改成这样的

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "label": "catkin_make:debug", //代表提示的描述性信息
            "type": "shell",  //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
            "command": "catkin_make",//这个是我们需要运行的命令
            "args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
            "group": {"kind":"build","isDefault":true},
            "presentation": {
                "reveal": "always"//可选always或者silence,代表是否输出信息
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

没注释版本:

{

    "version": "2.0.0",
    "tasks": [
        {
            "label": "catkin_make:debug", 
            "type": "shell", 
            "command": "catkin_make",
            "args": [],
            "group": {"kind":"build","isDefault":true},
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

在这里插入图片描述
这时,再按 Ctrl + Shift + B 就可以根据刚才的配置进行编译了,如下结果:
在这里插入图片描述

4.编写ros功能包

在这里插入图片描述
功能包名(自定义,这里我取的名字是cs_vscode
在这里插入图片描述
添加功能包依赖(上一步回车之后,框里面自动变的,注意框下面的提示,不要乱点)
在这里插入图片描述完成后可看见
在这里插入图片描述
这时可以 按下Ctrl+Shift+B 进行编译,虽然还没开始写文件,但是可以如果编译出错说明之前依赖包的名字出错了,这样可用于检测。
在这里插入图片描述

5.编写cpp文件

在src中添加一个叫cs.cpp的源文件,用于打印一句 hello_vscode
在这里插入图片描述

#include "ros/ros.h"

int main(int argc,char *argv[])
{

ros::init(argc,argv,"hello_vscode");

ROS_INFO("hello");

return 0;

}

以上代码建议手打,如果发现vscode无法自动补全,可以在c_cpp_properties.json处添加以下的配置保存后再试试(当然也可能是vscode启动的时候有个信任区的配置没弄对)
在这里插入图片描述

      "intelliSenseMode": "gcc-x64",
      "compilerPath": "/usr/bin/gcc",
      "cppStandard": "c++17"

在这里插入图片描述

6.设置编译配置文件

将下面的部分的注释去掉
注意CMakeLists.txt不要弄错了
在这里插入图片描述

然后把之前的源文件修改成自己的(如下我的是 cs.cpp),配置节点名称(如下我的是hello_cs)也是

在这里插入图片描述

7.编译并运行

同上还是按 Ctrl + Shift + B 进行编译
如下就是成功了
在这里插入图片描述
接着点击开启一个新终端准备运行
在这里插入图片描述
先拉起ros核心

roscore

在这里插入图片描述
然后再刷新环境变量

source ./devel/setup.bash

最后启动

rosrun cs_vscode hello_cs

如下图

在这里插入图片描述

命令行运行方式如下也是一样:

在这里插入图片描述

三.中文字体问题处理

根据该连接的操作,进行中文修复
——————————————————————————

如果无法下载里面的包尝试换源
初始源备个份

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://security.ubuntu.com/ubuntu/ xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu/ xenial-security main restricted
deb http://security.ubuntu.com/ubuntu/ xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu/ xenial-security universe
deb http://security.ubuntu.com/ubuntu/ xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu/ xenial-security multiverse

改成了个阿里源

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员进化不脱发!

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值