ubuntu+vscode断点调试c++程序

安装就再不提了,默认已经安装好ubuntu+vscode

创建文件夹

在这里插入图片描述

准备一段代码:

test.cpp:

#include <iostream>

using namespace std;

int main(){
    int a(0), b(1);
    cout<<a+b<<endl;
    return 0;
}

写CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(test)

set(CMAKE_BUILD_TYPE DEBUG)  

add_executable (test src/test.cpp)

断点调试时,一定要:set(CMAKE_BUILD_TYPE DEBUG)这句,发布时改为set(CMAKE_BUILD_TYPE RELEASE)

修改launch.json:

{
    // 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": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

在自动生成的launch.json下,修改内容为:
1、"program": "${workspaceFolder}/build/test",指明执行程序位置:build/test,
2、"preLaunchTask": "build",此处build只是一个label,可随便取,注意和后面的tasks.json中保持一致即可;

修改tasks.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "cd ${workspaceFolder}/build;cmake ..;make",
            "args": [
                
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

这也是自动生成的,修改部分为:
1、"type": "shell"
2、"label": "build",此处build和前面launch.json中的label保持一致。
3、"command": "cd ${workspaceFolder}/build;cmake ..;make"这个就是在终端执行

cd build
cmake ..
make

的意思,注意这个${workspaceFolder}"cwd": "${workspaceFolder}"中的这个东西,表示当前工作空间路径。
这下就完了,设置个断点一下试试吧:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值