手机版
你好,游客 登录 注册
背景:
阅读新闻

Ubuntu下VS Code如何调试C++代码

[日期:2018-11-17] 来源:Linux社区  作者:JsonZhangAA [字体: ]

最近开始在Ubuntu下使用Vs Codel,真的方便,可以和git结合。下面总结一下如何调试程序,

我写了一个实例程序(不重要)

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
    fstream iofile("test.txt");
    vector<string> strs(20);
    int i=0;
    if(!iofile){
        cerr<<"open file failed"<<endl;
    }else{
        while(iofile>>strs[i++]);
    }
    for(int j=0;j<i-1;j++)
    {
        cout<<strs[j]<<endl;
    }
//    cout<<endl;
    cout<<"after sort"<<endl;
    cout<<*(strs.begin())<<endl;
    sort(strs.begin(),strs.begin()+i-1);
    cout<<"i:"<<i<<" "<<strs[1]<<endl;
    for(int j=0;j<i;j++)
        {
                cout<<strs[j]<<" ";
        }
    cout<<endl;
    return 0;
}

这个时候,我们按F5,发现不能运行,它提示需要一个Launch.json文件,OK,这是一个启动文件,我们来配置它。

{
    // 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}/build",
            "preLaunchTask": "build",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

注意,这里需要修改的部分主要是program那一行,仅需修改为自己编译后产生的文件名,不如g++ -g 1.7.cpp -o build,所以这里我就取了build这个名字。

还有,就是要添加preLaunchTask这一行,名字与下面的task要对应。

这里,它还需要一个task.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",
                "${workspaceFolder}/chapter01/1.7.cpp",
                "-o",
                "build"
            ]
        }
    ]
}

大家可以看到,这里只需要将label的值要与上面的preLaunchTask相对应,其他的就是命令行部分,完成这个文件后,我们ctrl+shift+b,这是会执行task。

之后,我们打开main文件,设置断点,F5即可开始调试代码。

关于Launch.json:

官方是这样说的:However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details. VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings.但是,对于大多数调试方案,创建启动配置文件是有益的,因为它允许您配置和保存调试设置详细信息。 VS Code将配置信息保存在位于工作区(项目根文件夹)的.vscode文件夹或用户设置或工作区设置中的launch.json文件中。

个人感觉可能是项目比较简单所以看不来它的好处。

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-11/155374.htm

linux
相关资讯       VS Code调试C++代码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款