vscode+wsl调试c++程序

1. 在wsl中打开vscode

  1. 进入wsl,找到程序目录,在目录下打开vscode
> cd /mnt/d/vscode/
> code .

ok,打开了vscode,并且进入了wsl。在vscode的左下角可以看到:
在这里插入图片描述

2. 在vscode中配置c++环境

创建main.cpp文件:

#include<stdio.h>

int main()
{
    int a=0;
    printf("hello %d\r\n",a++);
}

使用快捷键Ctrl+Shift+P显示全部的命令。

2.1 配置c++环境(c_cpp_properties.json)

在这里插入图片描述
使用Ctrl+Shift+P,找到**C/C++:Edit Configuration (json)**点击,会自动创建一个c_cpp_properties.json文件,内容如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

2.2 配置编译任务(tasks.json)

使用Ctrl+Shift+P,找到Tasks:Configer Task点击,再点击Create tasks.json file from template,再选择others会自动创建一个tasks.json文件:

在这里插入图片描述

修改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","main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

2.3 配置调试环境

在这里插入图片描述
点击左侧的Debug图标,然后选择Add Configurations再选择C++(GDB/LLDB)

在这里插入图片描述

使用gcc-7 build and debug active filegcc build and debug active file会自动创建相应的调试配置文件launch.json。使用Default Configuration需要自己手动修改文件如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

3. 调试方式

  • F5 进入调试,或在下一个断点暂停
  • F10 单步调试
  • F11 进入方法
  • Shift + F5 停止调试
  • Shift + F11 退出方法
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值