工具

  • VSCode
  • 一台云服务器 (本文用阿里云 ECS 服务器)

环境

  • windows10
  • Ubuntu22.04.4

VSCode远程连接Linux服务器

配置SSH连接

搜索SSH,安装Remote - SSH插件

image-20250107112516138

右键底部勾选远程主机

image-20250107112730755

开始连接

image-20250107112830492

点击配置SSH主机

image-20250107114630289

点击第一个文件

image-20250107114648392

配置SSH文件,修改红框的内容

image-20250107122255673

连接刚刚配置好的Linux服务器

image-20250107122326372

点击Linux

image-20250107114940513

输入密码

image-20250107115011504

至此已经连接成功

image-20250107122508091

配置免密码登录

image-20250107123427722

绑定密钥对

image-20250107123441870

没有密钥的话就创建密钥,然后选择创建好的密钥

image-20250107123458390

把私钥放到相关的文件夹

image-20250107124357607

一般是C:\Users\Administrator\.ssh

image-20250107124435396

文件夹内容

image-20250107131451184

修改之前VSCODE中的SSH配置,添加IdentityFile

1
2
3
4
5
6
7
8
9
10
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host xxxxx
#HostName为主机的IP地址
HostName xx.xx.xx.xx
#User为登录Linux的用户
User ecs-user
#Port为连接的端口号
Port 22
# pem 文件的位置
IdentityFile C:\Users\Administrator\.ssh\lambda.pem

image-20250107124637238

C/C++编程

安装环境

确保安装了gcc

1
gcc -v

image-20250107134650138

安装GDB 调试器

1
2
sudo apt update # 更新软件包源 
sudo apt-get install build-essential gdb

VSCode中安装并配置开发插件

安装C++开发插件

image-20250107131833820

配置自动提示,CTRL+,

image-20250107132016756

输入以下内容

1
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
{
"workbench.iconTheme": "vscode-icons",
"editor.mouseWheelZoom": true,
"cSpell.languageSettings": [


],
"workbench.colorTheme": "Default Dark+",
"cmake.configureOnOpen": true,
"editor.minimap.enabled": true,
"C_Cpp.autocomplete": "Default",
"[cpp]": {
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},
"[c]": {
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},
"explorer.confirmDelete": false,
// 这个根据自己的情况来进行配置
"remote.SSH.remotePlatform": {
"学习公费服务器": "linux"
}
}

配置编译功能

先打开一个.cpp文件

image-20250107132319602

选择C/C++:g++生成活动任务

image-20250107133256998

上述操作将自动在.vscode文件夹下生成tasks.json文件。当然你也可以自己创建该文件

image-20250107133333601

1
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
41
42
43
44
45
46
47
48
49
50
51
{
"version": "2.0.0",
"tasks": [
//这个是C++的编译配置
{
"type": "cppbuild",
// 记住这个,等下有用
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: /usr/bin/g++"
},
//这个是C语言的编译配置
{
"type": "cppbuild",
"label": "C/C++: gcc 生成活动文件",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/gcc"
}
]
}

点击要编译的文件

image-20250107133411882

编译文件

image-20250107132623763

点击C/C++:g++生成活动任务

image-20250107133500037

生成成功

image-20250107133524895

运行编译后的文件

image-20250107133551994

输入./main

image-20250107133617247

查看运行结果

image-20250107133643574

调试Debug

image-20250107133741714

选择C++(GDB/LLDB)

image-20250107133823725

自动创建了文件launch.json

image-20250107134925725

点击添加配置,选择C/C++ (gdb) 启动

image-20250112134959092

修改launch.json

image-20250112135428158

1
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
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
//这是运行时添加的额外参数
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
//preLaunchTask位进行调试之前需要执行的任务,即编译构建生成
"preLaunchTask": "C/C++: g++ 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

image-20250107135753874

在需要调试的文件中按F5启动调试

image-20250107135927264

这里可以进行调试的相关操作

image-20250107140024813