vscode配置c/c++调试环境

本文记录win平台使用vscode远程连接ubuntu server服务器下,如何配置c/c++调试环境。

过程

1. 服务器配置编译环境

这里的前置条件是vscode已经能够连接到服务器,第一步安装编译构建套件(gcc、g++、make、链接器等)和调试器:sudo apt-get install build-essential gdb

2. 配置vscode

安装c/c++拓展
安装c/c++拓展
Ctril+Shift+P调出命令行,Reload Window快速重启窗口

生成.vscode文件夹下默认配置,主要包含如下三个文件:
在这里插入图片描述

Ctrl+Shift+P调出命令行:
命令行==>C/C++:编辑配置(JSON)				// c_cpp_properties.json
命令行==>任务:配置任务						// task.json
命令行==>调试:添加配置						//launch.json

C/C++编辑配置
任务:配置任务
调试:添加配置

3. 创建源文件,设置具体json配置项

源文件如下:

// main.cpp
#include <iostream>
#include <iomanip> // 用于控制输出格式

int main() {
    for (int i = 1; i <= 9; ++i) { // 外层循环,控制行
        for (int j = 1; j <= i; ++j) { // 内层循环,控制列
            // 打印乘法表的一项,setw(4)设置输出宽度为4,以保持对齐
            std::cout << j << "x" << i << "=" << std::setw(2) << i*j << "  ";
        }
        std::cout << std::endl; // 每打印完一行后换行
    }
    return 0;
}

配置项如下,说明以注释形式给出:

// c_cpp_properites.json 默认的
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}
// launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.dbg", // 调试程序名
            "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": "build_dbg" // 预执行任务
        }

    ]
}

// 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_dbg",
            "type": "shell",
            "command": "g++ *.cpp -g -o main.dbg" // 任务,这里使用g++编译输出文件
        }
    ]
}

下断点,F5调试,vsc将根据launch.json中的配置,使用gdb调试main.gdb,在此之前执行preLaunchTask,即"build_dbg","build_dbg"在task.json中配置,执行“g++ *.cpp -g -o main.dbg”
源文件

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值