使用vscode远程连接ubuntu开发调试

创建日期:2024-07-09
更新日期:2024-12-08

1、ubuntu上安装ssh。

sudo apt update
sudo apt install openssh-server
sudo systemctl status ssh
sudo systemctl start ssh
sudo systemctl enable ssh

2、vscode安装Remote - SSH扩展。

添加主机,在输入SSH连接命令对话框中输入以下命令,按Enter保存。

ssh liteng@192.168.153.129 -A

在远程资源管理器点刷新,在IP对应的主机后面点在当前窗口中连接,输入密码。

3、在虚拟机上安装开发工具。

sudo apt install build-essential
sudo apt install gdb

3、新建一个cpp后缀的文件,vscode会提示是否安装c++扩展,点击安装会自动安装C/C++Extension Pack。

hello.cpp

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello,world!" << endl;
}

4、添加启动配置文件和任务文件。

.vscode/launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

.vscode/tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

5、在代码里添加断点,点击运行、启动调试,就可以调试c++代码了。