vscode远程linux开发
# 前言
本文介绍在 windows 上使用 vscode 远程 linux 开发,包括 vscode 上使用 GDB 调试及跨平台编译工具 CMake 的使用。
# vscode 配置
linux 端需要开启 SSH
- 本地 vscode 安装
Remote Development
扩展
在 VScode 中
Ctrl + Shift + p
打开命令输入框,输入remote ssh
,在弹出的下拉列表中,选择Add New SSH Host
,输入ssh pi@192.168.91.102
(账户名@IP地址),回车然后选择ssh配置保存目录继续打开命令输入框,输入
remote ssh
,在弹出的下拉列表中,选择Connect to Host
,在弹出的输入框选择我们之前配置的主机地址,然后选择系统类型(Windows/Linux/macOS),根据实际情况选择即可,这里我们是 Linux,然后输入要远程登录的 linux 账户密码就好了。
连接成功之后,可以像在本地开发一样打开远程 linux 系统的任何文件夹,打开的终端也是 linux 上的。
- 安装
C/C++
、CMake
、CMake Tools
扩展,注意是安装到远程 linux 上
# 创建工程
这里的例子是远程到树莓派上开发的
- vscode 打开远程 linux 端的工作目录,比如这里是 /home/pi/work
- 新建项目目录
test
,并创建如下目录及文件
- test/src/add/add.c
int add(int a, int b)
{
return a + b;
}
2
3
4
- test/src/add/add.h
#ifndef ADD_H
#define ADD_H
int add(int a, int b);
#endif
2
3
4
5
6
- test/src/main.c
#include <stdio.h>
#include "add.h"
void main()
{
int result = 0;
printf("hello world!\n");
result = add(3, 5);
printf("Result is %d\n", result);
printf("hello raspberry pi!\n");
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- test/CMakeLists.txt
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.16)
#显示执行构建过程中详细的信息 (比如为了得到更详细的出错信息)
set(CMAKE_VERBOSE_MAKEFILE on)
# Declares and names the project.
project("HelloWorld")
# Release - Adds the -O3 -DNDEBUG flags to the compiler
# Debug - Adds the -g flag (default)
# MinSizeRel - Adds -Os -DNDEBUG
# RelWithDebInfo - Adds -O2 -g -DNDEBUG flags
set(CMAKE_BUILD_TYPE Debug)
# if (CMAKE_BUILD_TYPE MATCHES "Debug")
# set(CMAKE_BUILD_TYPE Release)
# endif()
# 声明头文件路径
include_directories(src/add)
# 声明源文件路径
aux_source_directory(src/add DIR_SRCS)
# 编译成可执行文件
add_executable(hello src/main.c ${DIR_SRCS})
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- 创建
test/build
目录
- vscode 上 Ctrl + ` 打开终端,然后执行如下命令生成 makefile
# 进入 build 目录
cd test/build/
# 执行 上一层目录中的 CMakeLists.txt
cmake ..
2
3
4
执行
make
命令进行编译构建可执行文件执行
./hello
文件,运行成功,正常输出。
至此,我们已经成功在本地 VScode 上远程到树莓派上使用 cmake 开发
# GDB 调试
- VScode 侧边栏选择
运行和调试按钮
,点击创建 launch.json 文件
,自动生成launch.json
文件,保存在.vscode
文件夹中
- 添加 GDB 调试 配置
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动", // 配置名称,显示在配置下拉菜单中
"type": "cppdbg", // 配置类型
"request": "launch", // 请求配置类型,可以是启动或者附加
// "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"program": "${workspaceFolder}/test/build/hello", // 可执行文件的完整路径,${workspaceFolder} 是 vscode 所打开的工作目录
"args": [], // 传递给可执行文件的命令行参数
"stopAtEntry": false, // 可选参数,如果为 true,调试程序应该在入口 main 处停止
"cwd": "${workspaceFolder}/test", // 目标的工作目录
"environment": [], // 表示要预设的环境变量
"externalConsole": false, // true 表示调试对象启动控制台
"MIMode": "gdb", // 要连接到的控制台启动程序
"setupCommands": [ // 为了安装基础调试程序而执行的一个或多个GDB/LLDB命令
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"miDebuggerPath": "/usr/bin/gdb", // GDB 路径
"preLaunchTask": "Build", // 启动前执行的任务
// 终端不再显示调试启动信息 ( 不显示类似这么一句:[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-5g2iv2qz.zpz" 1>"/tmp/Microsoft-MIEngine-Out-yuzstsba.n3i" )
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /bin/gdb -q --interpreter=mi"
}
]
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
vscode 也会预设一些默认配置给我们选择,
Ctrl + Shift + p
命令框中输入Debug
选择Debug: Add Configuration
,然后选择需要的配置
- 创建启动前需要执行的任务
在上面,我们通过
"preLaunchTask": "Build",
配置了 build 任务
在项目目录下的 .vscode
中 创建 tasks.json
{
"version": "2.0.0",
"options": {
// the task runner's current working directory upon the startup of VS Code,这里代表启动时终端 cd 到 build 目录下
"cwd": "${workspaceFolder}/test/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
],
"detail": "执行 cmake .. 命令"
},
{
"type": "shell",
"label": "make",
"command": "make",
"args": [],
"detail": "执行 make 命令"
},
{
"label": "Build", // 这个就是 启动前需要执行的任务名称
"dependsOrder": "sequence", // 按列出的顺序执行任务依赖项
"dependsOn": [
"cmake",
"make"
],
// 所属分组
"group": {
// 属于 build 分组
"kind": "build",
// 默认执行 (在命令面板里执行 "Tasks: Run Build Task" 时 (快捷键:Ctrl + Shift + B),会显示改分组下的任务,名字就是label,然后在选择就会执行这个任务,isDeisfault 就是不用选择,默认就会执行的任务 )
"isDeisfault": true
},
}
]
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
vscode 也会预设一些任务给我们选择,顶部菜单栏选择
终端
->配置任务
,然后选择需要的任务即可
- 启动
运行和调试界面,选择我们刚刚配置的启动项,启动并调试程序
# 说明
在本例中中,.vscode
文件夹并不在我们的项目目录中,因为生成的路径是我们打开的工作目录,这里我们打开的是 home/pi/Work
目录,所以 .vscode
文件夹跟项目目录在同一级。
如果你想要 .vscode
文件夹在项目目录下,那么你的 vscode 就打开 home/pi/Work/test
作为工作目录即可,这时候上面的一些跟路径有关的就要修改一下,比如:
launch.json
中
"program": "${workspaceFolder}/build/hello"
"cwd": "${workspaceFolder}"
tasks.json
中
"cwd": "${workspaceFolder}/build"